Hướng dẫn how do you transfer data from one python file to another? - làm thế nào để bạn chuyển dữ liệu từ tệp python này sang tệp khác?

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc

    Bàn luận

    Hướng dẫn how do you transfer data from one python file to another? - làm thế nào để bạn chuyển dữ liệu từ tệp python này sang tệp khác?

    Đưa ra hai tệp văn bản, nhiệm vụ là viết chương trình Python để sao chép nội dung của tệp thứ nhất vào tệp thứ hai.UsingFile handling to read and append

    Các tệp văn bản sẽ được sử dụng là thứ hai.txt và first.txt:

    Example:

    Python3

    with open('first.txt',

    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)
    0
    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)
    1open(
    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,
    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)
    6
    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)
    7

    Phương pháp số 1: Sử dụng xử lýFile để đọc và nối thêm

    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)
    
    3
    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)
    
    4

    Output:

    Hướng dẫn how do you transfer data from one python file to another? - làm thế nào để bạn chuyển dữ liệu từ tệp python này sang tệp khác?

    Chúng tôi sẽ mở đầu tiên.txt trong ‘R, Modeand sẽ đọc nội dung của First.txt. Sau đó, chúng tôi sẽ mở Second.txt trong chế độ ‘Một chế độ và sẽ nối nội dung của First.txt vào Second.txt.UsingFile handling to read and write

    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)
    8
    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)
    9
    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)
    
    0
    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)
    
    1
    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)
    
    2

    Example:

    Python3

    with open('first.txt',

    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)
    0
    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)
    1open(
    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,
    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)
    
    6
    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)
    7

    Phương pháp số 1: Sử dụng xử lýFile để đọc và nối thêm

    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)
    
    3
    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)
    
    4

    Output:

    Hướng dẫn how do you transfer data from one python file to another? - làm thế nào để bạn chuyển dữ liệu từ tệp python này sang tệp khác?

    Chúng tôi sẽ mở đầu tiên.txt trong ‘R, Modeand sẽ đọc nội dung của First.txt. Sau đó, chúng tôi sẽ mở Second.txt trong chế độ ‘Một chế độ và sẽ nối nội dung của First.txt vào Second.txt.Using shutil.copy() module

    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)
    8
    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)
    9
    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)
    
    0
    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)
    
    1
    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)
    
    2 method in Python is used to copy the content of the source file to destination file or directory. 

    Example:

    Python3

    Phương pháp số 2: Sử dụng xử lýFile để đọc và viết

    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)
    
    7'first.txt',
    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)
    4with 1

    Output:

    Hướng dẫn how do you transfer data from one python file to another? - làm thế nào để bạn chuyển dữ liệu từ tệp python này sang tệp khác?


    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.

    Để 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ắt đầu, hãy chụp đường dẫn nơi tệp của bạn hiện đang được lưu trữ.

    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:

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

    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.

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

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

    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:

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

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

    Đố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:

    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)

    Đả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:

    Cú pháp

    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'C:\Users\Ron\Desktop\Test_1\products.csv'
    target = r'C:\Users\Ron\Desktop\Test_2\products.csv'
    
    shutil.copyfile(original, target)
    

    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.

    Ngoài ra, bạn có thể sao chép một tệp có tên mới.

    Chẳng hạn, hãy để sao chép tệp CSV gốc (với tên tệp của ‘Sản phẩm) vào vị trí mới với tên tệp mới (‘ New_Products ‘):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)
    

    Tên tệp mới (được gọi là ‘new_products) sau đó sẽ được sao chép ở vị trí đích (thư mục test_2).new_products‘) would then be copied in the target location (the Test_2 folder).

    Các nguyên tắc tương tự sẽ áp dụng cho các loại tệp khác. Chẳng hạn, hãy để giả sử rằng một tệp JPG có tên ‘Hình ảnh‘ được lưu trữ trong thư mục Test_1.image‘ is stored in the Test_1 folder.

    Mã sau đây có thể được sử dụng để sao chép hình ảnh vào thư mục Test_2: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)
    

    Tệp JPG bây giờ sẽ xuất hiện trong thư mục Test_2.Test_2 folder.

    Làm cách nào để viết dữ liệu từ tệp này sang tệp khác?

    Đọc từng dòng..
    Mở File1, có nội dung ở chế độ đọc ..
    Mở File2 ở chế độ viết ..
    Sử dụng cho trong vòng lặp để đi qua mọi dòng trong File1 và ghi vào File2 ..
    Nội dung được ghi vào File2 ..
    Đóng cả hai tệp ..

    Làm thế nào để bạn sao chép dữ liệu trong Python?

    Các bước để sao chép một tập tin trong Python..
    Tìm đường dẫn của một tập tin.Chúng ta có thể sao chép một tệp bằng cả đường dẫn tương đối và đường dẫn tuyệt đối.....
    Sử dụng hàm SOWL.Copy ().....
    Sử dụng hàm Os.ListDir () và SOWLIL COPY () để sao chép tất cả các tệp.....
    Sử dụng hàm copytree () để sao chép toàn bộ thư mục ..