Làm cách nào để sao chép và đổi tên hình ảnh trong python?

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ệp

Thí 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

trước khi đổi tên một tập tin

Sau khi đổi tên

Sau khi đổi tên một tập tin

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

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

  • 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]
    6. Đường dẫn đến tệp cần đổ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]
    7. Đường dẫn đích cho tệp mới được đổ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]
    8. [Tùy chọn] Thư mục tệp nguồ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]
    9. [Tùy chọn] Thư mục tệp đích

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]
34

Sử 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

  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]
    35 trong điều kiệ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]
    36
  2. 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]
35

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

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

ví 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
    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]
    39. Nó trả về một danh sách chứa tên của các mục trong thư mục đã cho
  • 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]
7

Sau khi đổi tên tất cả các tập tin

Chỉ đổ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
    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

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 os

Phươ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]
21

Sử 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
    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]
    23
  • 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
    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]
    25 của lớ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]
    26 để thay thế phần mở rộng hiện có bằng phần mở rộng mới trong tên tệp
  • Cuối cùng, 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 để đổi tên cũ bằng tên mới

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]
21

Xem 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ệp

Cuố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]
5

Vấ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ục

os.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]
7

Trong 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

Tôi có thể sử dụng Python để đổi tên tệp không?

Python rename[] file là một phương thức dùng để đổi tên một file hoặc một thư mục trong lập trình Python và có thể được khai báo bằng cách chuyển hai đối số có tên src [Source] và dest [Destination .

Việc sử dụng đổi tên [] trong Python là gì?

rename[] trong Python được sử dụng để đổi tên tệp hoặc thư mục . Phương pháp này đổi tên tệp/thư mục nguồn thành tệp/thư mục đích được chỉ định.

Lệnh Copy trong Python là gì?

copyfile[] trong Python được sử dụng để sao chép nội dung của tệp nguồn sang tệp đích. Siêu dữ liệu của tệp không được sao chép. Nguồn và đích phải đại diện cho một tệp và đích phải có thể ghi được.

Chủ Đề