#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    Lopping multiple folders


    Hello,

    I have a script, which compares 2 directories, and performs a copy function. In this I have a source dir and a dest dir, and I am comparing both these. Now, I need to enhance this a bit. What if, I have multiple dest dir's and 1 source dir.
    I need the single source dir, to be compared with all the dest dir's one by one and perform the copy function. I tried using the loop, but i messing it up. Can you guys help out.
    Here's what I have,

    Code:
    import os.path
    import os
    import shutil
    
    source = r'\Path1'
    dest = r'Path\2'
    
    sourcefiles = {os.path.splitext(x)[0] for x in os.listdir(source) if os.path.splitext(x)[1] == '.ext1'}
    destfiles = {os.path.splitext(x)[0] for x in os.listdir(source) if os.path.splitext(x)[1] == '.ext2'}
    
    print os.getcwd()
    
    for missing in sourcefiles - destfiles:   # calculate the difference
        sourcefile = os.path.join(source, missing + '.ext2')
        destfile = os.path.join(dest, missing + '.ext2')
        shutil.copy(sourcefile, destfile)
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    Originally Posted by vincent.pinzon
    Hello,

    I have a script, which compares 2 directories, and performs a copy function. In this I have a source dir and a dest dir, and I am comparing both these. Now, I need to enhance this a bit. What if, I have multiple dest dir's and 1 source dir.
    I need the single source dir, to be compared with all the dest dir's one by one and perform the copy function. I tried using the loop, but i messing it up. Can you guys help out.
    Here's what I have,

    Code:
    import os.path
    import os
    import shutil
    
    source = r'\Path1'
    dest = r'Path\2'
    
    sourcefiles = {os.path.splitext(x)[0] for x in os.listdir(source) if os.path.splitext(x)[1] == '.ext1'}
    destfiles = {os.path.splitext(x)[0] for x in os.listdir(source) if os.path.splitext(x)[1] == '.ext2'}
    
    print os.getcwd()
    
    for missing in sourcefiles - destfiles:   # calculate the difference
        sourcefile = os.path.join(source, missing + '.ext2')
        destfile = os.path.join(dest, missing + '.ext2')
        shutil.copy(sourcefile, destfile)
    First of all, I don't know what using {} in what appears to be a list and not a dictionary does. I think it should fail.

    Second, you use "source" in both lists and I think you want to use "dest" in building "destfiles".

    So now, what if "dest" were a list:
    Code:
    dests=["path2","path3","path4"]
    note the "s"...dests.

    Wrap the entire process, from "sourcefiles=..." to the end, in a loop:

    Code:
    for dest in dests:
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by rrashkin
    First of all, I don't know what using {} in what appears to be a list and not a dictionary does. I think it should fail.

    Second, you use "source" in both lists and I think you want to use "dest" in building "destfiles".

    So now, what if "dest" were a list:
    Code:
    dests=["path2","path3","path4"]
    note the "s"...dests.

    Wrap the entire process, from "sourcefiles=..." to the end, in a loop:

    Code:
    for dest in dests:
    Yes, I have used source in both of them, and yes you are right about that. the script works fine, I have it tested.

    Now,
    Code:
    dests=["path2","path3","path4"]
    I can do this only if the list is static, first of all the list is dynamic, it keeps changing and second I'm trying to put this in a loop, so that, when I run the script, it visits each folder, and performs the copy function.

    Say, I have 6 folders, now the
    Code:
    dest = r'Path\2'
    needs to be in a loop right, somehow! I'm just going wrong with the logic
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    It's a set.


    Code:
    $ python3
    Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> type({1,2,3})
    <class 'set'>
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    157
    Rep Power
    2
    Originally Posted by b49P23TIvg
    Code:
    $ python3
    Python 3.2.3 (default, Oct 19 2012, 19:53:16) 
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> type({1,2,3})
    <class 'set'>
    >>>
    I didn't know that (I always used set(list)). Is this new to v3 or has it always been there and I just didn't know?
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Without investigating the subject, I recall that the set and tuple comprehensions were new in python 3000 also known as python3k now known as python3. I expect they'd also be part of python2.7 since the syntax doesn't break any code. I ignored python 2.6.
    Code:
    >>> {i**2 for i in range(-4,5)} # set comprehension
    set([16, 9, 4, 0, 1])
    >>> 
    >>> 
    >>> 
    >>> def f(iterator):
    ...     for i in iterator:
    ...         print(i,i*i)
    ... 
    >>> 
    >>> 
    >>> 
    >>> f(i*i for i in range(4))  # (I think) this is called tuple comprehension
    (0, 0)
    (1, 1)
    (4, 16)
    (9, 81)
    >>> 
    >>> 
    >>> 
    >>> sys.version
    '2.7.3 (default, Sep 26 2012, 21:51:14) \n[GCC 4.7.2]'

    Comments on this post

    • rrashkin agrees : as usual, knowledgeable and helpful
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo