Problem
Another day, my boss request me :
Can you make me a python script to move all file in "this" folder to "that" folder, so i can automate backup "this" folder, you can skip duplicate files to shorten process time
Logic
We can loop through all files in "this" folder, and check if is in "that" folder. If isn't in "that" folder, we use function "shutil.copy2" to move files to "that" folder. (We use copy2 because we want to preserve file metadata)
1. import library function "shutil" and "os"
2. Record path to "this" folder to variable "fromDirectory"3. Record path to "that" folder to variable "toDirectory"
4. Loop trough all files, and check with function "os.path.isfile"
5. Use function "shutil.copy2" to move if check is done
6. save python script in any folder you want
7. create task schedule for automate
Solution
Make new python with the following script :
import os import shutil fromDirectory = "'this' folder path" toDirectory = "'that' folder path" src_files = os.listdir(fromDirectory) for file_name in src_files: full_file_name_src = os.path.join(fromDirectory, file_name) full_file_name_des = os.path.join(toDirectory, file_name) if not (os.path.isfile(full_file_name_des)): if (os.path.isfile(full_file_name_src)): shutil.copy2(full_file_name_src, toDirectory)
save script, and make task schedule to autorun script
Tidak ada komentar:
Posting Komentar