Hướng dẫn copy in python - sao chép trong python

Trong hướng dẫn ngắn này, bạn sẽ thấy cách sao chép một tệp, từ thư mục này sang thư mục khác, sử dụng Python.

Nội dung chính ShowShow

  • Bước 1: Chụp đường dẫn ban đầu
  • Bước 2: Chụp đường đích
  • Bước 3: Sao chép tệp trong Python bằng cách sử dụng SOWL.CopyFile
  • Lệnh sao chép trong Python là gì?
  • Làm thế nào để bạn sao chép dữ liệu từ biến này sang biến khác trong Python?
  • Làm cách nào để sao chép tệp dữ liệu?
  • Làm cách nào để sao chép một tệp văn bản trong Python?

Để bắt đầu, đây là một mẫu mà bạn có thể sử dụng để sao chép một tệp trong Python bằng SOWLIL.CopyFile:

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be copied\file name.file extension'

shutil.copyfile(original, target)

Bây giờ, hãy xem các bước để áp dụng mẫu trên trong thực tế.

Bước 1: Chụp đường dẫn ban đầu

Bước 2: Chụp đường đích

Bước 3: Sao chép tệp trong Python bằng cách sử dụng SOWL.CopyFileTest_1:

Lệnh sao chép trong Python là gì?

Làm thế nào để bạn sao chép dữ liệu từ biến này sang biến khác trong Python?products‘ and the file extension is csv.

Bước 2: Chụp đường đích

Bước 3: Sao chép tệp trong Python bằng cách sử dụng SOWL.CopyFile

Lệnh sao chép trong Python là gì?Test_2:

Làm thế nào để bạn sao chép dữ liệu từ biến này sang biến khác trong Python?

Bước 3: Sao chép tệp trong Python bằng cách sử dụng SOWL.CopyFile

Lệnh sao chép trong Python là gì?

import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be copied\file name.file extension'

shutil.copyfile(original, target)

Làm thế nào để bạn sao chép dữ liệu từ biến này sang biến khác trong Python?r‘ character before your paths to avoid the following error:

Làm cách nào để sao chép tệp dữ liệu?

Làm cách nào để sao chép một tệp văn bản trong Python?

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\products.csv'
target = r'C:\Users\Ron\Desktop\Test_2\products.csv'

shutil.copyfile(original, target)

Để bắt đầu, đây là một mẫu mà bạn có thể sử dụng để sao chép một tệp trong Python bằng SOWLIL.CopyFile:products‘ CSV file would be copied into the Test_2 folder.

Bây giờ, hãy xem các bước để áp dụng mẫu trên trong thực tế.

Để bắt đầu, hãy chụp đường dẫn nơi tệp của bạn hiện đang được lưu trữ.products‘) to the new location with a new file name (‘new_products‘):

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\products.csv'
target = r'C:\Users\Ron\Desktop\Test_2\new_products.csv'

shutil.copyfile(original, target)

Ví dụ: hãy giả sử rằng một tệp CSV được lưu trữ trong một thư mục có tên là Test_1:Test_1:new_products‘) would then be copied in the target location (the Test_2 folder).

C: \ Users \ Ron \ Desktop \ test_1 \ Products.csvimage‘ is stored in the Test_1 folder.

Trong đó tên tệp CSV là ‘Sản phẩm‘ và phần mở rộng tệp là CSV.products‘ and the file extension is csv.Test_2 folder:

import shutil

original = r'C:\Users\Ron\Desktop\Test_1\image.jpg'
target = r'C:\Users\Ron\Desktop\Test_2\image.jpg'

shutil.copyfile(original, target)

Tiếp theo, chụp đường dẫn đích nơi bạn muốn sao chép tệp.Test_2 folder.

Ví dụ của chúng tôi, tệp sẽ được sao chép vào một thư mục có tên Test_2:Test_2:

shutil_methods =
{'copy':['shutil.copyfileobj',
          'shutil.copyfile',
          'shutil.copymode',
          'shutil.copystat',
          'shutil.copy',
          'shutil.copy2',
          'shutil.copytree',],
 'move':['shutil.rmtree',
         'shutil.move',],
 'exception': ['exception shutil.SameFileError',
                 'exception shutil.Error'],
 'others':['shutil.disk_usage',
             'shutil.chown',
             'shutil.which',
             'shutil.ignore_patterns',]
}

C: \ Users \ Ron \ Desktop \ test_2 \ Products.csv

  1. Đối với bước cuối cùng, hãy sử dụng mẫu sau để sao chép tệp của bạn:
In [3]: src = '~/Documents/Head+First+SQL.pdf'
In [4]: dst = '~/desktop'
In [5]: shutil.copyfileobj(src, dst)
AttributeError: 'str' object has no attribute 'read'
#copy the file object
In [7]: with open(src, 'rb') as f1,open(os.path.join(dst,'test.pdf'), 'wb') as f2:
    ...:      shutil.copyfileobj(f1, f2)
In [8]: os.stat(os.path.join(dst,'test.pdf'))
Out[8]: os.stat_result(st_mode=33188, st_ino=8598319475, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067347, st_mtime=1516067335, st_ctime=1516067345)
  1. Đảm bảo đặt ký tự ‘R‘ trước các đường dẫn của bạn để tránh lỗi sau:r‘ character before your paths to avoid the following error:
In [9]: shutil.copyfile(src, dst)
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
  1. Cú pháp
import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be copied\file name.file extension'

shutil.copyfile(original, target)
1
  1. Trong bối cảnh ví dụ của chúng tôi, mã hoàn chỉnh sẽ trông như thế này:
import shutil

original = r'original path where the file is currently stored\file name.file extension'
target = r'target path where the file will be copied\file name.file extension'

shutil.copyfile(original, target)
3
  1. import shutil
    
    original = r'original path where the file is currently stored\file name.file extension'
    target = r'target path where the file will be copied\file name.file extension'
    
    shutil.copyfile(original, target)
    4

Nếu bạn chạy mã trong Python (được điều chỉnh theo đường dẫn của bạn), bạn sẽ thấy rằng tệp CSV của sản phẩm sẽ được sao chép vào thư mục Test_2.products‘ CSV file would be copied into the Test_2 folder.

Lệnh sao chép trong Python là gì?

Làm thế nào để bạn sao chép dữ liệu từ biến này sang biến khác trong Python?used to copy the content of the source file to the destination file. The metadata of the file is not copied. Source and destination must represent a file and destination must be writable.

Làm thế nào để bạn sao chép dữ liệu từ biến này sang biến khác trong Python?

Làm cách nào để sao chép tệp dữ liệu?using the “copy()” function.

Làm cách nào để sao chép tệp dữ liệu?

Làm cách nào để sao chép một tệp văn bản trong Python?Right-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V . There will now be a copy of the file in the original folder and the other folder.

Làm cách nào để sao chép một tệp văn bản trong Python?

Để bắt đầu, đây là một mẫu mà bạn có thể sử dụng để sao chép một tệp trong Python bằng SOWLIL.CopyFile:shutil. copy() method in Python is used to copy the content of the source file to destination file or directory.