Bạn có thể sử dụng mẫu sau để đổi tên tệp bằng Python:
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']
Bây giờ, hãy xem xét một ví dụ với các bước để đổi tên một tệp bằng Python.
Giả sử rằng mục tiêu của bạn là đổi tên một tệp văn bản từ các sản phẩm của thành phố thành các sản phẩm được vận chuyển.
Dưới đây là các bước mà bạn có thể làm theo để đổi tên tệp của mình:
Bước 1: Chụp đường dẫn nơi lưu trữ tệp
Để bắt đầu, hãy chụp đường dẫn nơi tập tin của bạn được lưu trữ.
Đối với các mục đích trình diễn, hãy để giả sử rằng một tệp có tên là Sản phẩm, được lưu trữ theo đường dẫn sau:Products” is stored under the following path:
C: \ Users \ ron \ Desktop \ test
Lưu ý rằng bạn sẽ cần sửa đổi đường dẫn tệp để phản ánh vị trí lưu trữ tệp trên máy tính của bạn.
Bước 2: Đổi tên tệp
Để đổi tên tệp bằng Python, bạn sẽ cần nhập gói HĐH.
Sau đó, bạn có thể sử dụng mẫu sau để đổi tên tệp của mình:
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']
Trong bối cảnh ví dụ của chúng tôi:
- Đường dẫn tệp: C: \ Users \ ron \ Desktop \ test: C:\Users\Ron\Desktop\Test
- Tên tệp cũ: Sản phẩm: Products
- Tên tệp mới: Sản phẩm được vận chuyển: Shipped Products
- Loại tệp: TXT: txt
Don Tiết quên đặt ra R Rv trước đường dẫn tệp để tránh lỗi sau trong Python:r” before the file path to avoid the following error in Python:
.
Mã Python hoàn chỉnh để đổi tên tệp văn bản từ các sản phẩm của Cameron thành các sản phẩm được vận chuyển trên mạng là:
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
Chạy mã [được điều chỉnh theo đường dẫn tệp của bạn] và bạn sẽ nhận được tên tệp mới:
Sản phẩm vận chuyển
Bước tùy chọn: Thêm dấu ngày khi đổi tên tệp
Hãy nói rằng bạn muốn thêm một dấu ngày khi đổi tên tệp.
Bạn có thể hoàn thành nhiệm vụ này bằng gói DateTime.
Trong ví dụ của chúng tôi, để đổi tên tệp sản phẩm của Google thành các sản phẩm được vận chuyển trên mạng + Dấu ngày, bạn sẽ cần áp dụng mã này [được điều chỉnh theo đường dẫn tệp của bạn]:
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
Chạy mã và bạn sẽ nhận được tệp đổi tên này với ngày:
Sản phẩm vận chuyển_07-Aug-2021
Một vài ghi chú về mã:
- %d-%b-%y & nbsp; phản ánh định dạng ngày của dd-mmm-yyyy reflects the date format of dd-mmm-yyyy
- Biểu tượng + được sử dụng để kết hợp tên tệp mới, dấu ngày và loại tệp TXT+ symbol is used to concatenate the new file name, the date stamp and the txt file type
Làm cách nào để đổi tên một tập tin trong khi tải xuống Python?
Các bước để đổi tên tệp trong Python.: –
- Tìm đường dẫn của một tập tin để đổi tên. Để đổi tên một tập tin, chúng tôi cần đường dẫn của nó. Đường dẫn là vị trí của tệp trên đĩa. ....
- Quyết định một tên mới. Lưu một tên cũ và một tên mới trong hai biến riêng biệt. old_name = 'chi tiết.txt' ....
- Sử dụng phương thức đổi tên [] của một mô -đun HĐH. Sử dụng hệ điều hành ..
- Làm thế nào để bạn đổi tên một tập tin trong Python?
- Để đổi tên các tệp theo Python, hãy sử dụng phương thức đổi tên [] của mô -đun HĐH. Các tham số của phương thức đổi tên [] là địa chỉ nguồn [tên cũ] và địa chỉ đích [tên mới].
Làm cách nào để sao chép và đổi tên một tệp trong Python?
Python - Sao chép tệp và đổi tên để sao chép một tệp từ vị trí này sang vị trí khác và đổi tên tệp trong thư mục đích, sử dụng thư viện Shutil Python. giao thoa. Sao chép [Nguồn, Target] sao chép tệp nguồn vào đường dẫn tệp được chỉ định bởi Target. trong đó nguồn và mục tiêu là tuyệt đối hoặc đường dẫn tệp tương đối.
- Tìm đường dẫn của một tệp để đổi tên
Để đổi tên một tập tin, chúng tôi cần đường dẫn của nó. Đường dẫn là vị trí của tệp trên đĩa. Một đường dẫn tuyệt đối chứa danh sách thư mục hoàn chỉnh cần thiết để định vị tệp. Một đường dẫn tương đối chứa thư mục hiện tại và sau đó là tên tệp.
An absolute path contains the complete directory list required to locate the file.
A relative path contains the current directory and then the file name. - Quyết định một tên mới
Lưu tên cũ và một tên mới trong hai biến riêng biệt .________ 22
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
3import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
2import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
3 - Sử dụng phương thức đổi tên [] của mô -đun HĐH
Sử dụng phương thức
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
4 để đổi tên tệp trong thư mục. Chuyển cả tên cũ và tên mới cho hàmimport os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
5 để đổi tên tệp.
Ví dụ: Đổi tên một tập tin bằng Python
Trong ví dụ này, chúng tôi đang đổi tên chi tiết về chi tiết.
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]
Output::
Trước khi đổi tên
Sau khi đổi tên
import os
os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
4
Như được hiển thị trong ví dụ, chúng ta có thể đổi tên một tệp trong Python bằng phương thức ________ 27 [] có sẵn trong mô -đun HĐH. Mô -đun
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']8 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 thuộc các mô -đun tiện ích tiêu chuẩn 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 vượt qua cho phương thức
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']4
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
0: Đường dẫn cho tệp phải được đổi tênimport os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
1: Đường dẫn đích cho tệp mới được đổi tênimport os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
2: [tùy chọn] Thư mục tệp nguồnimport os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
3: [tùy chọn] Thư mục tệp đích
Lưu ý: Nếu import os
import datetime
Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y']
os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
1 đã tồn tại thì import os
import datetime
Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y']
os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
5 sẽ được ném vào Windows và trong trường hợp UNIX, import os
import datetime
Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y']
os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
6 sẽ bị ném.: If the
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']1 already exists then the
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']5 will be thrown in Windows and in the case of UNIX an
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']6 will be thrown.
Đổi tên một tệp sau khi kiểm tra xem nó có tồn tại không
Phương thức
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']4 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 gói mã của chúng tôi trong khối
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']8.
Sử dụng hàm ISFILE [‘Path,] trước khi đổi tên một tệp. Nó trả về đúng 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 đây để 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.
- Sử dụng
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']
9 trong điều kiện
0import 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]
- Viết đổi tên mã trong khối Excet Try-Except.
Ví dụ 1: Sử dụng
import os import datetime Current_Date = datetime.datetime.today[].strftime ['%d-%b-%Y'] os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products_' + str[Current_Date] + '.txt']9
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
The file already exists
Ví dụ 2: cùng một mã có thể được bọc trong khối Excet Try-Except như dưới đây.: The same code could be wrapped in the try-except block as below.
import os
old_name = r"E:\demos\files\reports\details.txt"
new_name = r"E:\demos\files\reports\new_details.txt"
# enclosing inside try-except
try:
os.rename[old_name, new_name]
except FileExistsError:
print["File already Exists"]
print["Removing existing file"]
# skip the below code
# if you don't' want to forcefully rename
os.remove[new_name]
# rename it
os.rename[old_name, new_name]
print['Done renaming a file']
Đầu ra::
File already Exists Removing existing file Done renaming a file
Đổ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.rename all files from a directory. Consider a folder with four files with different names, and we wanted to rename all file names.
Chúng ta có thể đổi tên nhiều tệp trong một thư mục bằng phương thức
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']4 bằng cách làm theo các bước dưới đây.
- Nhận danh sách các tệp trong một thư mục bằng
3. Nó trả về một danh sách chứa tên của các mục trong thư mục đã cho.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]
- Lặp lại trong danh sách bằng cách sử dụng một vòng lặp để truy cập từng tệp một
- Đổi tên từng tệp
Ví dụ sau đây cho thấy cách thay đổi tên của tất cả các tệp từ một thư mục.
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']0
Đầu ra
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']1
Đầu ra:
Đổ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 ta có thể đổi tên nhiều tệp trong một thư mục bằng phương thức
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
4 bằng cách làm theo các bước dưới đây. - Nhận danh sách các tệp trong một thư mục bằng
3. Nó trả về một danh sách chứa tên của các mục trong thư mục đã cho.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]
- Lặp lại trong danh sách bằng cách sử dụng một vòng lặp để truy cập từng tệp một
Example::
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']2
Đầu ra
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']3
Ví dụ 2: cùng một mã có thể được bọc trong khối Excet Try-Except như dưới đây.
Đầu ra:
Đổ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 ta có thể đổi tên nhiều tệp trong một thư mục bằng phương thức
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
4 bằng cách làm theo các bước dưới đây. - Nhận danh sách các tệp trong một thư mục bằng
3. Nó trả về một danh sách chứa tên của các mục trong thư mục đã cho.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]
- Lặp lại trong danh sách bằng cách sử dụng một vòng lặp để truy cập từng tệp một
Đổi tên từng tệp
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']4
Ví dụ sau đây cho thấy cách thay đổi tên của tất cả các tệp từ một thư mục.
Sau khi đổi tên tất cả các tệp
Đổi tên chỉ một danh sách các tệp trong thư mục
Trong khi đổi tên các tệp bên trong một thư mục, đôi khi chúng ta có thể chỉ cần đổi tên một danh sách các tệp, không phải tất cả các tệp. Sau đây là các bước chúng tôi cần tuân theo để chỉ đổi tên một tập hợp các tệp bên 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 qua danh sách các tệp trong thư mục chứa các tệp
- Lặp lại thông qua các tệp trong thư mục
- Thay đổi tên theo quy ước đặt tên mới.
Ví dụ: Đổi tên tất cả các tệp văn bản bắt đầu với từ Sales Sales Bán hàng bên trong thư mục Báo cáo trên mạng với tên mới là doanh thu và một bộ đếm.: Rename all text files starting with the word “sales” inside the “reports” folder with the new name “revenue” and a counter.
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']5
Đầu ra
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']6
Đổi tên phần mở rộng của các tệp
Chúng ta chỉ có thể thay đổi phần mở rộng của các tệp bằng 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]
5. Đ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ỉ nhận tên tệp bằng phương thức splitExt [] của mô -đun HĐH.Phương thức này trả về rễ và phần mở rộng riêng biệt. Khi chúng ta nhận được gốc/cơ sở của tên tệp, chúng ta có thể thêm tiện ích mở rộng mới vào nó trong khi đổi tên nó bằng 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]
5Sử dụng các bước dưới đây để đổi tên chỉ tiện ích mở rộng: -
- Nhận tên tệp danh sách từ một thư mục bằng cách sử dụng
7import 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ừ một danh sách tên tệp
- Xây dựng tên tệp hiện tại bằng phương thức ____48 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
9 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]
0 để thay thế tiện ích mở rộng hiện có bằng tiện ích mở rộng mới trong tên tệpos.rename[src, dst, *, src_dir_fd=None, dst_dir_fd=None]
- Cuối cùng, sử dụng
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']
4 để đổi tên một tên cũ có tên mới
Hãy cùng xem ví dụ.
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']7
Đầu ra
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']8
import os
os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']
6
Đổi tên phần mở rộng của các tệp
Chúng ta chỉ có thể thay đổi phần mở rộng của các tệp bằng 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]
5. Đ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ỉ nhận tên tệp bằng phương thức splitExt [] của mô -đun HĐH.Phương thức này trả về rễ và phần mở rộng riêng biệt. Khi chúng ta nhận được gốc/cơ sở của tên tệp, chúng ta có thể thêm tiện ích mở rộng mới vào nó trong khi đổi tên nó bằng 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]
5import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']9
Sử dụng các bước dưới đây để đổi tên chỉ tiện ích mở rộng: -
Nhận tên tệp danh sách từ một thư mục bằng cách 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]
7Tiếp theo, lặp lại từng tệp từ một danh sách tên tệpĐầu ra
import os os.rename[r'C:\Users\Ron\Desktop\Test\Products.txt',r'C:\Users\Ron\Desktop\Test\Shipped Products.txt']1
import os os.rename[r'file path\OLD file name.file type',r'file path\NEW file name.file type']6