Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
239 views
in Technique[技术] by (71.8m points)

Recursive copying of files and folders and subfolders with python

I would like to recursively traverse the source directory.

  • If a folder exists in the source directory and is missing in the target, a folder with the same name should be created in the target.
  • If a file exists in the source directory and is missing in the target it should be copied.
  • If a file already exists in the destination directory, the checksum of the source file should be calculated. For each file in the target there is a file with the same name and the extension .sha512 which contains the checksum of the target file. The calculated target file and the checksum read from the file are to be compared:
    1. If the checksum of source and destination file is the same nothing should happen
    1. If the checksums are different, the destination file should be renamed (by appending a date to the end of the file), the file with the checksum should be deleted in the destination. Then copy the source file to the destination (with the original file name) and create a new file with the checksum (same name as the source file + file extension .sha512). This means that in the target (except for the files with the checksums) the files are never deleted but a history is kept.

In the target there is a file with the checksum (same file name + file extension .sha512) for each file, because the connection is established via the Internet and is therefore not the fastest. This saves time in calculating the checksum in the target.

What have I tried / achieved so far?

  • A function which calculates the checksums
  • A loop which runs through the folders and files recursively
  • Writing a file with the checksum

My problems or questions:

  • How can I create missing folders in the target at the "right place" in the target?
  • How can I copy files to the right place in the target?

My problem is that in my attempts, all files and folders are written to the root directory of the target. So no structure is created and the files (no matter where they are in the source) are written to the main folder of the destination.

Can you please show me how to create folders and copy files accordingly?

I would be glad about any help.

Thank you very much.

In the source directory are the following sample folders and sample files:

C:sourceinfo.txt
C:sourceTest.docx
C:sourcefolder1info.txt
C:sourcefolder1Test.xlsx
C:sourcefolder2info.txt
C:sourcefolder2Test.xlsx
C:sourcefolder1folderAinfo.txt
C:sourcefolder1folderATest.xlsx
C:sourcefolder2folderBinfo.txt
C:sourcefolder2folderBTest.xlsx

In the target directory are the following sample folders and sample files:

C:sourceinfo.txt
C:sourceinfo.txt.sha512
C:sourcefolder1info.txt
C:sourcefolder1info.txt.sha512
C:sourcefolder1info-20200112.txt
C:sourcefolder1info-20190108.txt
C:sourcefolder2Test.xlsx
C:sourcefolder2Test.xlsx.sha512
C:sourcefolder1folderATest.xlsx
C:sourcefolder1folderATest-20201112.xlsx
C:sourcefolder1folderATest-20201212.xlsx
C:sourcefolder2folderBTest.xlsx.sha512
C:sourcefolder1folderATest.xlsx
C:sourcefolder2folderBTest.xlsx.sha512

This is my approach which works so far that everything is run through recursively:

source_path = "C:\source\"
target_path = "C:\target\"

for dirpath, dirnames, filenames in os.walk(source_path, topdown=True):
    for dir in dirnames:
        # What code is missing at this point?
    for file in filenames:
        # What code is missing at this point?
question from:https://stackoverflow.com/questions/65853486/recursive-copying-of-files-and-folders-and-subfolders-with-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...