Skip to content

Instantly share code, notes, and snippets.

@cballenar
Created September 18, 2023 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cballenar/0621d78774cac9643510f9fe87072136 to your computer and use it in GitHub Desktop.
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.
"""
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