April 29th, 2013, 07:02 AM
-
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)
April 29th, 2013, 09:17 AM
-
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:
April 29th, 2013, 09:35 AM
-
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:
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 needs to be in a loop right, somehow! I'm just going wrong with the logic
April 29th, 2013, 09:43 AM
-
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!
April 30th, 2013, 09:28 AM
-
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?
April 30th, 2013, 10:01 AM
-
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
[code]
Code tags[/code] are essential for python code and Makefiles!