I am writing a script which will automatically copy files from my working directory into shared drives for others to grab and use. Depending on the project I am working on, there is a specific place in the shared drives for it to go.
For example:
If I am working on a project called "Banana", I want to be able to run my script within my local "Banana" directory. The script calls for a single argument. The argument is the name of the project.
Example:
>python C:\Users\me\projects\Banana\myCopyScript.py Banana
Upon doing so, it should automatically copy all my "Banana" project files into shared drives and into the folder for that project.
Example:
>python C:\Users\me\projects\Banana\myCopyScript.py Banana
...Copying Banana to T:\SharedDrive1\projects\Banana
...Copying Banana to Z:\SharedDrive2\projects\Banana
Right now, the script works only if I go in and manually enter the file path locations (where to copy from and where to copy to).
However, I would like to create a file that I can import into this script file. This import file would contain a list of all the Projects and the file paths it should copy to/from.
Example .xml file:
<project>Banana</project>
<copyTo>T:\SharedDrive1\projects\Banana</copyTo>
<copyTo>Z:\SharedDrive2\projects\Banana</copyTo>
<project>Apple</project>
<copyTo>T:\SharedDrive1\projects\Apple</copyTo>
<copyTo>Z:\SharedDrive2\projects\Apple</copyTo>
<project>Orange</project>
<copyTo>T:\SharedDrive1\projects\Orange</copyTo>
<copyTo>Z:\SharedDrive2\projects\Orange</copyTo>
I'm not set on an .xml file. It could be any kind of file. That element is part of the overall question I am asking. I considered JSON and YAML as well. I would like to know which format best suits this kind of data.
Right now, my script has the file paths hardcoded like so:
def main():
parser = argparse.ArgumentParser(description = 'copy targets out to their folders in the T: and Z: drives)
parser.add_argument('project', help='The project must be a folder in the current directory containing the files you want to copy')
args = parser.parse_args()
project_name = args.project
# TODO : figure out how to grab the local directory automatically instead of hard coding it each time
directory = r'C:\Users\me\projects\Banana'
for f in os.listdir(directory):
out_files.append(directory + '\\' +f)
# TODO : figure out how to reference an import file with all the projects and their file paths instead of hard coding it each time
for target_folder in (r'\\SharedDrive1\projects',
r'\\SharedDrive2\projects'):
make_folder(target_folder + '\\' + project_name)
print >> sys.stdout, 'Copying project folder', project_name, 'to', target_folder + '\\' + project_name
print (" ")
copy_files(out_files, target_folder + '\\' + project_name)
But this isn't ideal. Which is the second part of the question I am asking here. I want to be able to clean this up, but I don't have much background in this sort of thing. It is my first attempt at writing a script which will traverse directories and manipulate files/folders.