Làm cách nào để thay đổi phần mở rộng của nhiều tệp trong python?

Xin chào,

Tôi muốn thay đổi phần mở rộng tập tin từ. nén vào. pptx bằng công cụ python trong Alteryx

i. e. "SỬA ĐỔI. zip" thành "SỬA ĐỔI. pptx"

Đây là kịch bản

from ayx import Alteryx
import os
from pathlib import Path

directory = ['C:/Temp/PowerPoint test/DELETE_modified/DELETE_original/']

for f in Path[directory].rglob['.zip']:
    f.rename[directory + f.stem + '.pptx']

Tập lệnh chạy thành công, nhưng tệp zip vẫn giữ nguyên

Sử dụng phương pháp

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
0 để đổi tên tệp trong thư mục. Truyền cả tên cũ và tên mới cho hàm
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
1 để đổi tên tệp

Thí dụ. Đổi tên tệp trong Python

Trong ví dụ này, chúng tôi đang đổi tên “detail. txt” thành “new_details. txt”

import os

# Absolute path of a file
old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

# Renaming the file
os.rename[old_name, new_name]

đầu ra

Trước khi đổi tên

trước khi đổi tên một tập tin

Sau khi đổi tên

Sau khi đổi tên một tập tin

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
0

Như trong ví dụ, chúng ta có thể đổi tên một tệp trong Python bằng cách sử dụng phương thức

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
3[] có sẵn trong mô-đun os. Mô-đun
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
4 cung cấp các chức năng để tương tác với các hệ điều hành. Mô-đun này nằm trong các mô-đun tiện ích tiêu chuẩn của Python

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]

Sau đây là các tham số mà chúng ta cần truyền cho phương thức

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
0

  • import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    6. Đường dẫn đến tệp cần đổi tên
  • import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    7. Đường dẫn đích cho tệp mới được đổi tên
  • import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    8. [Tùy chọn] Thư mục tệp nguồn
  • import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    9. [Tùy chọn] Thư mục tệp đích

Ghi chú. Nếu

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
7 đã tồn tại thì
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
41 sẽ được ném vào Windows và trong trường hợp của UNIX, một
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
42 sẽ được ném

Đổi tên tệp sau khi kiểm tra xem nó có tồn tại không

Phương thức

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
0 làm tăng FileExistsError hoặc OSError khi tên tệp đích đã tồn tại. Điều này có thể tránh được bằng cách bọc mã của chúng ta trong khối
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
44

Sử dụng hàm isfile[‘path’] trước khi đổi tên tệp. Nó trả về true nếu tệp đích đã tồn tại

Chúng ta có thể sử dụng hai cách tiếp cận sau để tiếp tục đổi tên bằng cách xóa tệp cũ hoặc dừng mà không đổi tên tệp

  1. Sử dụng
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    45 trong điều kiện
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    46
  2. Viết mã đổi tên trong khối thử ngoại trừ

ví dụ 1. Sử dụng

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
45

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]

đầu ra

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
4

ví dụ 2. Mã tương tự có thể được bọc trong khối thử ngoại trừ như bên dưới

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
3

đầu ra

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
4

Đổi tên nhiều tệp trong Python

Đôi khi chúng ta cần đổi tên tất cả các tệp từ một thư mục. Hãy xem xét một thư mục có bốn tệp có tên khác nhau và chúng tôi muốn đổi tên tất cả các tên tệp

Chúng tôi có thể đổi tên nhiều tệp trong một thư mục bằng phương pháp

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
0 bằng cách thực hiện theo các bước bên dưới

  • Lấy danh sách các tệp trong một thư mục bằng cách sử dụng
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    49. Nó trả về một danh sách chứa tên của các mục trong thư mục đã cho
  • Lặp lại danh sách bằng cách sử dụng vòng lặp để truy cập từng tệp một
  • Đổi tên từng tệp

Ví dụ sau minh họa cách thay đổi tên của tất cả các tệp từ một thư mục

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
7

đầu ra

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
8

Sau khi đổi tên tất cả các tập tin

Chỉ đổi tên một danh sách các tệp trong một thư mục

Trong khi đổi tên các tệp trong một thư mục, đôi khi chúng ta có thể chỉ đổi tên một danh sách các tệp chứ không phải tất cả các tệp. Sau đây là các bước chúng ta cần thực hiện để chỉ đổi tên một tập hợp các tệp trong một thư mục

  • Cung cấp danh sách các tệp cần được đổi tên
  • Lặp lại danh sách các tệp trong thư mục chứa các tệp
  • Kiểm tra xem tệp có trong danh sách không
  • Nếu có, hãy đổi tên tệp theo quy ước mong muốn. Khác, di chuyển đến tập tin tiếp theo

Thí dụ

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
9

đầu ra

import os

# Absolute path of a file
old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

# Renaming the file
os.rename[old_name, new_name]
0

Đổi tên tệp bằng dấu thời gian

Trong một số ứng dụng, dữ liệu hoặc nhật ký sẽ được lưu trữ trong các tệp thường xuyên trong một khoảng thời gian cố định. Đó là một quy ước tiêu chuẩn để thêm dấu thời gian vào tên tệp để giúp chúng dễ dàng lưu trữ và sử dụng sau này. Trong Python, chúng ta có thể sử dụng mô-đun datetime để làm việc với ngày và giờ

Vui lòng làm theo các bước dưới đây để thêm dấu thời gian vào tên tệp

  • Nhận dấu thời gian hiện tại bằng mô-đun ngày giờ và lưu trữ dấu thời gian đó trong một biến riêng
  • Chuyển đổi dấu thời gian thành một chuỗi
  • Nối dấu thời gian vào tên tệp bằng cách sử dụng toán tử nối
  • Bây giờ, đổi tên tệp bằng tên mới bằng cách sử dụng
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    0

Xem xét ví dụ sau khi chúng tôi thêm dấu thời gian ở định dạng “%d-%b-%Y”

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
0

Đổi tên tệp bằng Mẫu

Đôi khi chúng tôi chỉ muốn đổi tên những tệp khớp với một mẫu cụ thể. Ví dụ: chỉ đổi tên các tệp pdf hoặc đổi tên các tệp có chứa một năm cụ thể trong tên của chúng

Khớp mẫu được thực hiện bằng mô-đun toàn cầu. Mô-đun toàn cầu được sử dụng để tìm các tệp và thư mục có tên theo một mẫu cụ thể

Chúng tôi có thể đổi tên các tệp khớp với mẫu bằng các bước sau. –

  • Viết một mẫu sử dụng các ký tự đại diện
  • Sử dụng phương thức global[] để tìm danh sách các tệp khớp với một mẫu
  • Lặp lại qua các tệp trong thư mục
  • Đổi tên theo quy ước đặt tên mới

Thí dụ. Đổi tên tất cả các tệp văn bản bắt đầu bằng từ “bán hàng” bên trong thư mục “báo cáo” bằng tên mới “doanh thu” và bộ đếm

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
1

đầu ra

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
2

Đổi tên phần mở rộng của tệp

Chúng tôi chỉ có thể thay đổi phần mở rộng của tệp bằng phương pháp

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
31. Điều này được thực hiện bằng cách lấy danh sách các tệp và sau đó chỉ lấy tên tệp bằng cách sử dụng phương thức splitext[] của mô-đun os

Phương thức này trả về gốc và phần mở rộng riêng biệt. Khi chúng tôi nhận được gốc/cơ sở của tên tệp, chúng tôi có thể thêm phần mở rộng mới cho nó trong khi đổi tên nó bằng phương thức

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
31

Sử dụng các bước bên dưới để chỉ đổi tên tiện ích mở rộng. –

  • Nhận danh sách tên tệp từ một thư mục bằng cách sử dụng
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    33
  • Tiếp theo, Lặp lại từng tệp từ danh sách tên tệp
  • Xây dựng tên tệp hiện tại bằng cách sử dụng phương pháp ______234 bằng cách chuyển tên tệp và đường dẫn
  • Bây giờ, hãy sử dụng phương thức
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    35 của lớp
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    36 để thay thế phần mở rộng hiện có bằng phần mở rộng mới trong tên tệp
  • Cuối cùng, sử dụng
    import os
    
    old_name = r"E:\demos\files\reports\details.txt"
    new_name = r"E:\demos\files\reports\new_details.txt"
    
    if os.path.isfile[new_name]:
        print["The file already exists"]
    else:
        # Rename the file
        os.rename[old_name, new_name]
    0 để đổi tên cũ bằng tên mới

Hãy xem ví dụ

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
3

đầu ra

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
4

Đổi tên và sau đó di chuyển tệp đến vị trí mới

Với sự trợ giúp của phương thức

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
31, chúng ta có thể đổi tên một tệp và sau đó di chuyển tệp đó đến một vị trí mới. Điều này được thực hiện bằng cách chuyển vị trí mới cho tham số
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
7 của phương thức
import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
31

Xem xét ví dụ dưới đây, nơi chúng tôi đang xác định hai thư mục khác nhau làm nguồn và đích riêng biệt. Sau đó, sử dụng

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
41, chúng tôi sẽ thay đổi tên và vị trí của tệp

Cuối cùng, khi chúng tôi in các tệp ở vị trí mới, chúng tôi có thể thấy tệp trong danh sách

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
5

Vấn đề thực hành. Đổi tên một tập tin hình ảnh

Chúng tôi có thể đổi tên bất kỳ tệp nào trong một thư mục và tệp thuộc bất kỳ loại nào bằng cách sử dụng

import os

old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"

if os.path.isfile[new_name]:
    print["The file already exists"]
else:
    # Rename the file
    os.rename[old_name, new_name]
0. Trong ví dụ dưới đây, chúng tôi sẽ thay đổi tên của tệp hình ảnh bên trong thư mục

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
6

đầu ra

os.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
7

Trong bài viết này, chúng tôi đã đề cập đến những điều cơ bản về đổi tên tệp, phương pháp được sử dụng để đổi tên. Chúng tôi cũng đã xem cách đổi tên tệp theo một mẫu cụ thể, đặt tên cho tất cả các tệp trong một thư mục và thêm ngày vào tệp. Chúng tôi cũng có ví dụ về cách thay đổi phần mở rộng của tệp và cách di chuyển tệp đến vị trí mới sau khi thay đổi tên

Chủ Đề