Shutil module replacing file irrespective of extension

This is the place for queries that don't fit in any of the other categories.

Shutil module replacing file irrespective of extension

Postby Ani Shetty » Mon Apr 22, 2013 12:03 pm

Hello,

I have a problem, I had to copy files from 1 directory to another if both the directories have files with the same name. The first dir will have files with .ext1 and the second one will files with .ext2. But the file names would be the same. So, when I copy files from the first dir to the second dir I dont want the files to be be replaced, rather I want both files with the same name but with different .ext to be present in the dest dir.
I have written the script, but I have used the shutil module. This copies the file to the dest dir but it replaces the file if it has the same name. It doesn't take the .ext into consideration. Both these files have different .ext. :(

Is there a way, to copy the file from source to dest, wherein the dest dir will contain both files with different extensions.

Would appreciate any help. Thank you!
Ani Shetty
 
Posts: 5
Joined: Mon Apr 22, 2013 11:58 am

Re: Shutil module replacing file irrespective of extension

Postby metulburr » Mon Apr 22, 2013 12:21 pm

This copies the file to the dest dir but it replaces the file if it has the same name. It doesn't take the .ext into consideration. Both these files have different .ext.

i think we are going to need some code shown, because i do not get such a result. I get both files.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1331
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Shutil module replacing file irrespective of extension

Postby Ani Shetty » Mon Apr 22, 2013 12:44 pm

Here's what I've got:

Code: Select all
import os.path
import os
import shutil

source = r'Path'
dest = r'Path'

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'}

for missing in sourcefiles - destfiles:
    sourcefile = os.path.join(source, missing + '.ext1')
    destfile = os.path.join(dest, missing + '.ext2')
    shutil.copy(sourcefile, destfile)

Ani Shetty
 
Posts: 5
Joined: Mon Apr 22, 2013 11:58 am

Re: Shutil module replacing file irrespective of extension

Postby setrofim » Mon Apr 22, 2013 12:57 pm

  • You probably want to be taking the intersection() rather than subtracting the two sets, as you've stated you only want to copy the file if it in both.
  • Since you do not want to overwrite the files in dest, your destfile when copying should have '.ext1'
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Shutil module replacing file irrespective of extension

Postby Ani Shetty » Mon Apr 22, 2013 1:14 pm

setrofim wrote:
  • Since you do not want to overwrite the files in dest, your destfile when copying should have '.ext1'


Well! of course that was stupid of me! :oops: Thanks a lot! It worked :D
Ani Shetty
 
Posts: 5
Joined: Mon Apr 22, 2013 11:58 am

Re: Shutil module replacing file irrespective of extension

Postby Ani Shetty » Mon Apr 22, 2013 1:26 pm

Thanks! I'm curious:

Currently what I have got, has both the source and destination defined.
What if I have more than 1 destination directories. I want the source file to be compared with multiple destination folders. The destination directories also have an extension. The source directory has to be compared with all these dest dir with an .ext and perform the same function. How do I loop it?
Ani Shetty
 
Posts: 5
Joined: Mon Apr 22, 2013 11:58 am

Re: Shutil module replacing file irrespective of extension

Postby setrofim » Mon Apr 22, 2013 1:47 pm

Something like (untested):
Code: Select all
import os.path
import os
import shutil

source = r'Path'
dest_paths = [
    ('path1', '.ext1'),
    ('path2', '.ext2'),
    ('path3', '.ext3'),
]

sourcefiles = {os.path.splitext(x)[0] for x in os.listdir(source) if os.path.splitext(x)[1] == '.ext1'}

for dest, ext in dest_paths:
    destfiles = {os.path.splitext(x)[0] for x in os.listdir(dest)
                 if os.path.splitext(x)[1] == ext}

    for missing in sourcefiles.intersection(destfiles):
        sourcefile = os.path.join(source, missing + '.ext1')
        destfile = os.path.join(dest, missing + '.ext1')
        shutil.copy(sourcefile, destfile)

setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Shutil module replacing file irrespective of extension

Postby Ani Shetty » Mon Apr 22, 2013 1:55 pm

Can't I just put the dest dir in a loop. In that case I don't have to manually update the dir everytime.

Code: Select all
Path = os.getcwd()
count = 0
os.chdir(Path + "\\*.ext")

I'm working on something like this, I just have to put the dest path in a loop. Any ideas? :(
Ani Shetty
 
Posts: 5
Joined: Mon Apr 22, 2013 11:58 am

Re: Shutil module replacing file irrespective of extension

Postby setrofim » Mon Apr 22, 2013 2:16 pm

That os.chdir() won't work but you can use glob to get a list of all entries matching '\\*.txt'. Generally, it is better to specify the paths as arguments to functions rather than chdir into them anyway.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot] and 5 guests