Hello everybody !

wow my first time on danyweb ! ;p

Well, here the deal.
Im doing a script that basically copy and past into the local drive a specified directory with all this files.
Im doing the copy with a copytree which is working well.
But, (and I know it will not work as copytree dont update directory) Im, like, stuck when I have to update the directory.

For a better understanding here what the tree look like:

G:/prod/actual/project/xxx/shot/shot_EN_006/render/v001

and sometime as we have to rerender, we update the "render" folder with a v002:

G:/prod/actual/project/xxx/shot/shot_EN_006/render/v002


So, actually my script can copy my G:/prod/actual/project/xxx/shot/shot_EN_006/render/v001 directory to C:/prod/actual/project/xxx/shot/shot_EN_006/render/v001
but I dont know how to proceed to update this folder when asked.

Here the actual script, as you can see the update part will not work.

# -*- coding: utf-8 -*-

import os
import sys
import threading
import shutil

def checkRead():

    networkMount = 'D:/'
    localMount = 'C:/'

    
    for item in sys.argv:
        project = (sys.argv[2])
        path = (sys.argv[3])

    workPath = '/Prod//Projects/' + '/' + project + '/' +  '/shots/' + '/' + path + '/' + '/render/' + '/REN/'
    
    localPath = localMount + workPath
    networkPath = networkMount + workPath

    if sys.argv[1] == 'update':
        if os.listdir(networkPath) != os.listdir(localPath):
            copyfile = shutil.copyfile(networkPath, localPath)
            
    elif sys.argv[1] == 'copy':    
        if os.path.exists(localPath) == 0:
            copytree = shutil.copytree(networkPath, localPath, symlinks=True, ignore=None)

    if sys.argv[1] not in  ('update', 'copy'):
        print 'Error, please enter the command: "update" or "copy"'
 

if __name__ == "__main__":
    checkRead()

Anyone can help me to figure out a solution please ?.

Thank you and have a good day !

Recommended Answers

All 10 Replies

Are you on windows?

The path is a backslash, and you have to escape it
\\

Hello,

Yup Iam on windows :) the path is working well (the script can copy the file but cant update it), but I take not for the backslash thank you.

But, just curious, if you have to escape the backslash to define a path how do you do it ?

Always use os.path.join!
It's much easier and there's less room for error. workPath = os.path.join('Prod','Projects',project,'shots',path,'render','REN')

Also, isn't copyfile for copying files, not directories?
Try using copytree.

I tried both, copyfile and copytree. I think I will have to do it with another command.

What do you mean by update? Copying newer version over old version? Not touching files with same name as exist in destination, but copying files with new name? How about files deleted from source?

Have you by the way looked my code snippet on watching change in list of directories?

I think update means overwriting the old ones with the new ones.

Hello everybody, thanks for your helping those by updating I meant, I want the script just look at the directory tree on local path and compare it with the networkpath. if there is a difference, like a v002 folder, or some overwrited files (maybe to compare with the creation date ?) it will update the localpath with those files.

its what those lines should do:

if sys.argv[1] == 'update':        
    [B]if os.listdir(networkPath) != os.listdir(localPath):[/B]
         copyfile = shutil.copyfile(networkPath, localPath)

By the way tonyjv, I didnt found your snippet code about change within directory, is this available somewhere please ?

Anyway thank you for your help guys.

I rarely use the os module for some reason,
so I can't help you much with this.
You can use os.walk to compare both directories.

My snippet is http://www.daniweb.com/code/snippet282009.html It should be usefull to at least sort out kind of events you expect to happen.

Would need to add the time of file creation or something in addition to names for that solution. I am not sure how the script react if whole directory itself disappears suddenly. Also copying must copy original date/time from source file. The solution is also for specified list of directories, not recursively to subdirectories (which would complicate things enormously, if directory names change). Maybe you need some kind of check sum check for renaming case, but maybe you can recognize case when same size and datetime file appears same time as old one disappears from directory.

Is local directory ever changed and need to copy files to server side? What about deleting file either side? Renaming, moving, new file in either directory ....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.