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ệpThí 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
Sau khi đổ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]
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]
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 Pythonos.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
6. Đường dẫn đến tệp cần đổi tênimport 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ênimport 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ồnimport 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 đíchimport 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]
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]
44Sử 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
- Sử dụng
45 trong điều kiệnimport 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]
46import 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]
- 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]
45import 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]
4ví 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
49. Nó trả về một danh sách chứa tên của các mục trong thư mục đã choimport 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]
- 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]
8Chỉ đổ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
0import 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]
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 osPhươ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]
31Sử 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
33import 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]
- 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
35 của lớpimport 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ệpimport 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]
- Cuối cùng, sử dụng
0 để đổi tên cũ bằng tên mớiimport 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]
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]
31Xem 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ệpCuố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]
5Vấ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ụcos.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]
7Trong 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