Sự mô tả
Phương thức đổi tên [] đổi tên tệp hoặc thư mục src thành dst.if dst là một tệp hoặc thư mục [đã có], Oserror sẽ được nâng lên.rename[] renames the file or directory src to dst.If dst is a file or directory[already present], OSError will be raised.
Cú pháp
Sau đây là Phương thức Đổi tên [] -rename[] method −
os.rename[src, dst]
Thông số
SRC - Đây là tên thực tế của tệp hoặc thư mục. − This is the actual name of the file or directory.
DST - đây là tên mới của tệp hoặc thư mục. − This is the new name of the file or directory.
Giá trị trả về
Phương pháp này không trả về bất kỳ giá trị nào.
Thí dụ
Ví dụ sau đây cho thấy cách sử dụng phương thức đổi tên [].
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]
Kết quả
Khi chúng tôi chạy chương trình trên, nó tạo ra kết quả sau -
The dir is: [ 'Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 'java.ppt', 'Python3' ] Successfully renamed. the dir is: [ 'Applicationdocs.docx', 'book.zip', 'foo.txt', 'Java Multiple Inheritance.htm', 'Java Multiple Inheritance_files', 'java.ppt', 'python2' ]
python_files_io.htm
Kể từ Python 3.4, người ta có thể sử dụng mô -đun Pathlib để giải quyết vấn đề này.
Nếu bạn có phiên bản cũ hơn, bạn có thể sử dụng phiên bản có backport được tìm thấy ở đây
Giả sử bạn không ở trong đường dẫn gốc [chỉ để thêm một chút khó khăn cho nó] bạn muốn đổi tên và phải cung cấp một đường dẫn đầy đủ, chúng ta có thể xem xét điều này:
some_path = 'a/b/c/the_file.extension'
Vì vậy, bạn có thể lấy đường dẫn của mình và tạo một đối tượng
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]5 ra khỏi nó:
from pathlib import Path
p = Path[some_path]
Chỉ để cung cấp một số thông tin xung quanh đối tượng này mà chúng ta có bây giờ, chúng ta có thể trích xuất mọi thứ từ nó. Ví dụ: nếu vì bất kỳ lý do gì, chúng tôi muốn đổi tên tệp bằng cách sửa đổi tên tệp từ
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]6 thành
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]7, thì chúng tôi có thể nhận phần tệp:
name_without_extension = p.stem
Và vẫn giữ phần mở rộng trong tay:
ext = p.suffix
Chúng tôi có thể thực hiện sửa đổi của mình bằng một thao tác chuỗi đơn giản:
Python 3.6 và sử dụng dây F lớn hơn!
new_file_name = f"{name_without_extension}_1"
Otherwise:
new_file_name = "{}_{}".format[name_without_extension, 1]
Và bây giờ chúng tôi có thể thực hiện đổi tên của mình bằng cách gọi phương thức
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]8 trên đối tượng đường dẫn chúng tôi đã tạo và nối thêm
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]9 để hoàn thành cấu trúc đổi tên phù hợp mà chúng tôi muốn:
p.rename[Path[p.parent, new_file_name + ext]]
Sớm hơn để thể hiện sự đơn giản của nó:
Python 3.6+:
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]0
Các phiên bản nhỏ hơn Python 3.6 Sử dụng phương thức định dạng chuỗi thay thế:
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]1
Tệp python rename [] là một phương thức được sử dụng để đổi tên tệp hoặc thư mục trong lập trình Python. Phương thức tệp python redmame [] có thể được khai báo bằng cách truyền hai đối số có tên SRC [nguồn] và DST [đích]. is a method used to rename a file or a directory in Python programming. The Python rename[] file method can be declared by passing two arguments named src [Source] and dst [Destination]. Đây là phương thức cú pháp cho os.rename [] SRC: Nguồn là tên của tệp hoặc thư mục. Nó nên tồn tại. Source is the name of the file or directory. It should must
already exist.Cú pháp
# !/usr/bin/python3
import os, sys
os.chdir["d:\\tmp"]
# listing directories
print ["The dir is: %s"%os.listdir[os.getcwd[]]]
# renaming directory ''tutorialsdir"
os.rename["python3","python2"]
print ["Successfully renamed."]
# listing directories after renaming "python3"
print ["the dir is: %s" %os.listdir[os.getcwd[]]]
2Thông số
DST: Destination là tên mới của tệp hoặc thư mục bạn muốn thay đổi. Destination is the new name of the file or directory you want to change.
Example:
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]3
Hãy xem xét chi tiết ví dụ
Bạn có thể đổi tên tệp gốc, chúng tôi đã thay đổi tên tệp từ Gur Gur99.txt, thành sự nghiệp.
- Để đổi tên tệp GURU99.TXT
- Vì vậy, khi mã được thực thi, bạn có thể quan sát rằng một tệp mới là Career.guru99.txt, được tạo ở phía bên phải của bảng điều khiển, mà chúng tôi đổi tên cho tệp gốc của chúng tôi.
Đây là mã hoàn chỉnh
# !/usr/bin/python3 import os, sys os.chdir["d:\\tmp"] # listing directories print ["The dir is: %s"%os.listdir[os.getcwd[]]] # renaming directory ''tutorialsdir" os.rename["python3","python2"] print ["Successfully renamed."] # listing directories after renaming "python3" print ["the dir is: %s" %os.listdir[os.getcwd[]]]4