Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách tạo file trùng lặp từ file có sẵn bằng Python?


Module shutil cung cấp hàm copy file và cả thư mục.

Gọi shutil.copy(source, destination) copy file tại source vào thư mục destination. (Cả hai là string.) Nếu destination là tên file, dùng làm tên mới file copy. Hàm trả về string đường dẫn file đã copy. Ví dụ:

>>> import shutil
>>> # Copy file cùng thư mục tên khác
>>> shutil.copy('original.txt', 'duplicate.txt')
'/home/username/duplicate.txt'
>>> shutil.copy('original.txt', 'my_folder/duplicate.txt')
'/home/username/my_folder/duplicate.txt'