hệ điều hành. đổi tên [src, dst]. src là địa chỉ nguồn của tệp được đổi tên và dst là đích với tên mới
Bây giờ, giả sử n hình ảnh đã cho trong một thư mục có tên ngẫu nhiên. Ví dụ, hãy xem xét hình ảnh dưới đây
Bây giờ yêu cầu là đổi tên chúng theo thứ tự như hostel1, hostel2, …v.v. Thực hiện việc này theo cách thủ công sẽ là một nhiệm vụ tẻ nhạt nhưng mục tiêu này có thể đạt được bằng cách sử dụng các phương thức rename[] và listdir[] trong mô-đun os.
Phương thức listdir liệt kê tất cả nội dung của một thư mục nhất định
Cú pháp cho listdir[].
danh sách = os. listdir['src']. trong đó src là thư mục nguồn sẽ được liệt kê
Đoạn mã sau sẽ thực hiện công việc cho chúng tôi. Nó duyệt qua danh sách tất cả các hình ảnh trong thư mục xyz, xác định địa chỉ đích [dst] và nguồn [src] và đổi tên bằng cách sử dụng mô-đun đổi tê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. Module 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 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]
31 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]
32 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]
34Sử 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
35 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]
36import 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]
35import 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]
3ví 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
# 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]
2đầ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]
3Đổ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
39. 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
# 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]
6đầ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]
7Chỉ đổ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
# 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]
8đầ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]
9Đổ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
# 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]
21. Đ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 pháp
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]
21Sử dụng các bước dưới đây để 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
23import 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]
- 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 thức ____224 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
25 của lớpimport 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]
26 để 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 # 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]
- 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 pháp
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]
21, chúng tôi có thể đổi tên 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
# 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]
21Xem 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
# 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]
31, 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