0

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.

Radical Edward
  • 115
  • 1
  • 11

1 Answers1

0

Maybe this can help you:

def getdict(var1,var2,var3):
    """
    var3 = 0 - read the dict (var1 = row, var2 = col)
    var3 = 1 - search the dict for the key provided in var1 and return [row,col]
    var3 = 2 - return dict len
    """
    dict = {
        0:["item_00","item_01"],
        1:["item_10","item_11"],
        2:["item_20","item_21"],
    }
    if var3 == 0: #read
        return dict[var1][var2]
    elif var3 == 1: #search
        for key, value in dict.items():
            idx = []
            if value[0] == var1:
                idx = [key,0]
                return idx
            elif value[1] == var1:
                idx = [key,1]
                return idx
    elif var3 == 2: #len
        return len(dict)

You can save it in a file and import.

You can update the dict inside the function writing it in a text file and saving as .py (I prefer using csv module). Just read items before writing and treat dict items inside a loop:

import csv

def writefile(fileline,file):
    text_file = open(file, "w")
    text_file.write(fileline)
    text_file.close()

txtline = 'def getdict(var1,var2,var3):\n'
txtline = textline + '    """\n'
...

(treat inside a loop)
0:["item_00","item_01"],
1:["item_10","item_11"],
2:["item_20","item_21"],
...

writefile(txtline,filename)

You can insert items, just get dict len.

Finally you can reload it:

How do I unload (reload) a Python module?

mecpod
  • 1
  • 2