Created
September 18, 2023 13:56
-
-
Save cballenar/0621d78774cac9643510f9fe87072136 to your computer and use it in GitHub Desktop.
Copy Files Based on Lists of Source and Target Locations A script to move files from their respective source location (s) to the desired target location (t). Where each s and t locations are stored as separate lists of paths, each in a separate line, in text files, sources.txt and targets.txt respectively.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Copy Files Based on Lists of Source and Target Locations | |
A script to move files from their respective source location (s) to the desired | |
target location (t). Where each s and t locations are stored as separate lists | |
of paths, each in a separate line, in text files, sources.txt and targets.txt respectively | |
""" | |
import os | |
import shutil | |
# let's make sure we log everything that happens here | |
import logging | |
Log_Format = "%(levelname)s %(asctime)s - %(message)s" | |
logging.basicConfig(filename = "logfile.log", | |
filemode = "w", | |
format = Log_Format, | |
level = logging.ERROR) | |
logger = logging.getLogger() | |
# Ok, let's continue with the script | |
sources_file = "source-data.txt" | |
targets_file = "target-data.txt" | |
source_path_prefix = "/full-path/source-directory/" | |
target_path_prefix = "/full-path/target-directory/" | |
# opening both the files in reading modes | |
with open(sources_file) as sources, open(targets_file) as targets: | |
# iter over both files simultaneously | |
for (source_line, target_line) in zip(sources, targets): | |
# get respective content, strip it, and prefix it | |
source_path = source_path_prefix + source_line.strip() | |
destination_path = target_path_prefix + target_line.strip() | |
# print contents for logging | |
logger.error("Copying from:{} to:{}.".format(source_path,destination_path)) | |
# check for directory and create if necessary | |
try: | |
os.makedirs(os.path.dirname(destination_path), exist_ok=True) | |
except Exception as e: | |
logger.error(e) | |
# test if the dest file exists, if false | |
if not os.path.exists(destination_path): | |
# copy file to its final destination | |
try: | |
shutil.copyfile(source_path, destination_path) | |
logger.error("Success!") | |
except Exception as e: | |
logger.error(e) | |
# else log an error | |
else: | |
logger.error("File already exist, copy aborted.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment