Làm cách nào bạn có thể đổi tên và xóa một tệp trong chương trình của mình bằng python?

Python là công cụ hoàn hảo để tự động hóa tổ chức tệp. Trong bài viết này, bạn sẽ học cách sử dụng Python để đổi tên, di chuyển, sao chép và xóa tệp và thư mục

Tôi cũng bao gồm một tập lệnh ví dụ để dọn dẹp Màn hình của bạn

Cách điều hướng và thay đổi thư mục trong Python

Bạn có thể sử dụng

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
9 để lấy thư mục làm việc hiện tại và
import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
0 để chuyển sang thư mục khác

import os

print[os.getcwd[]]

os.chdir["/Users/patrick/Desktop/video-files"]

print[os.getcwd[]]
# /Users/patrick/Desktop/video-files

Cách đổi tên tệp trong Python

Bạn có thể sử dụng

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
0 hoặc
import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
1

Ví dụ này thay đổi tên tệp từ

  • 'từ điển - python-course-3. di chuyển'

đến -->

  • '03-cua-tu-khoa-trăn. di chuyển'

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]

Hoặc như sự thay thế hiện đại, bạn có thể sử dụng

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
2

import os
from pathlib import Path

for file in os.listdir[]:
    f = Path[file]

    # split into base name and extension
    name, ext = f.stem, f.suffix

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    f.rename[new_name]

Cách tạo thư mục

Bạn có thể sử dụng

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
3 hoặc
import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
4

________số 8

Cách di chuyển tệp trong Python

Điều này có thể được thực hiện với mô-đun

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
5 và
import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
6

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
1

Cách sao chép tệp trong Python

Điều này có thể được thực hiện với

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
7 hoặc
import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
8. Cái sau cũng sao chép tất cả siêu dữ liệu

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
4

Cách xóa tệp và thư mục trong Python

Đối với các tệp, chúng tôi có thể sử dụng

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
9 và cho các thư mục trống
import os
from pathlib import Path

for file in os.listdir[]:
    f = Path[file]

    # split into base name and extension
    name, ext = f.stem, f.suffix

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    f.rename[new_name]
0. Để xóa đệ quy các thư mục không trống, chúng ta có thể sử dụng
import os
from pathlib import Path

for file in os.listdir[]:
    f = Path[file]

    # split into base name and extension
    name, ext = f.stem, f.suffix

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    f.rename[new_name]
1

import os

for file in os.listdir[]:
    # split into base name and extension
    name, ext = os.path.splitext[file]

    splitted = name.split["-"]
    splitted = [s.strip[] for s in splitted]
    new_name = f"{splitted[3].zfill[2]}-{splitted[1]}-{splitted[2]}-{splitted[0]}{ext}"

    os.rename[file, new_name]
8

Sắp xếp và dọn dẹp Desktop của bạn bằng Python

Tập lệnh ví dụ này có thể được sử dụng để di chuyển hình ảnh, video, ảnh chụp màn hình và tệp âm thanh vào các thư mục tương ứng

Một cách tuyệt vời để chạy tập lệnh này tự động là sử dụng công việc định kỳ. Trong bài viết này, bạn có thể tìm hiểu cách lên lịch các tập lệnh Python thành các công việc định kỳ với crontab [Mac/Linux]

Tôi có thể sử dụng Python để đổi tên tệp không?

Python rename[] file là một phương thức dùng để đổi tên một file hoặc một thư mục trong lập trình Python và có thể được khai báo bằng cách chuyển hai đối số có tên src [Source] và dest [Destination .

Phương pháp nào được sử dụng để xóa một tệp trong Python?

remove[] trong Python được sử dụng để xóa hoặc xóa đường dẫn tệp.

Chủ Đề