Hướng dẫn how do you rename a file in python 3? - làm cách nào để đổi tên tệp trong python 3?



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).

Cú pháp

Đây là phương thức cú pháp cho os.rename ()

# !/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()))
2

Thông số

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.

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.

Hướng dẫn how do you rename a file in python 3? - làm cách nào để đổi tên tệp trong python 3?

  • Để đổ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

Làm thế nào để bạn đổi tên một tên tệp trong Python?

1 câu trả lời..
Đầu tiên chúng tôi nhận được một danh sách các tên tệp trong thư mục ..
Sau đó, chúng tôi chuyển sang thư mục mong muốn ..
Sau đó, chúng tôi lặp lại thông qua các tên tệp ..
Chương trình sẽ cố gắng thay thế bất kỳ trường hợp 'kiểm tra' nào trong mỗi tên tệp bằng 'Trái đất'.
Sau đó, nó sẽ đổi tên các tệp với 'kiểm tra' trong tên thành phiên bản bằng 'kiểm tra' đã thay thế ..

Làm cách nào để đổi tên một tập tin?

Đổi tên một tập tin..
Trên thiết bị Android của bạn, mở các tệp của Google ..
Ở phía dưới, nhấn Duyệt ..
Nhấn vào một danh mục hoặc một thiết bị lưu trữ.Bạn sẽ thấy các tệp từ danh mục đó trong danh sách ..
Bên cạnh một tệp bạn muốn đổi tên, nhấn vào mũi tên xuống.Nếu bạn không nhìn thấy mũi tên xuống, hãy nhấn Danh sách xem ..
Nhấn Đổi tên ..
Nhập một tên mới ..
Nhấn OK ..

Làm cách nào để xóa và đổi tên một tệp trong Python?

Đổi tên và xóa các tập tin trong Python..
hệ điều hành.Đổi tên (current_file_name, new_file_name).
#!/usr/bin/python nhập hệ điều hành # đổi tên một tệp từ test1.txt thành os test2.txt.Đổi tên ("test1.txt", "test2.txt").
hệ điều hành.Xóa (File_Name).
#!/usr/bin/python nhập hệ điều hành # xóa tệp test2.txt os.Xóa ("Text2.txt").

Làm cách nào để đổi tên tệp TXT?

Đổi tên một tập tin hoặc thư mục..
Nhấp chuột phải vào mục và chọn Đổi tên hoặc chọn tệp và nhấn F2 ..
Nhập tên mới và nhấn Enter hoặc nhấp vào Đổi tên ..