[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

copying files through Python

Lalit

2/13/2008 9:51:00 PM

I am new to python. Infact started yesterday and feeling out of place.
I need to write a program which would transfer files under one folder
structure (there are sub folders) to single folder. Can anyone give me
some idea like which library files or commands would be suitable for
this file transfer task. I am sure its matter of few commands. It
would be even more great if I could get some sample code with
instructions

Thanks
8 Answers

Tim Chase

2/13/2008 10:14:00 PM

0

> I am new to python. Infact started yesterday and feeling out of place.
> I need to write a program which would transfer files under one folder
> structure (there are sub folders) to single folder. Can anyone give me
> some idea like which library files or commands would be suitable for
> this file transfer task. I am sure its matter of few commands. It
> would be even more great if I could get some sample code with

The shutils.copytree() command[1] will do what you describe

Depending on your source, and if you want to make the dest
writeable (such as copying off a CD), you may also need to use
os.walk() [2] combined with os.chmod(filepath, stat.S_IWRITE) [3]

-tkc

[1]
http://docs.python.org/lib/module-shutil.htm...

[2]
http://docs.python.org/lib/os-file-dir.htm...

[3]
http://docs.python.org/lib/os-file-dir.htm...

Larry Bates

2/13/2008 10:18:00 PM

0

Lalit wrote:
> I am new to python. Infact started yesterday and feeling out of place.
> I need to write a program which would transfer files under one folder
> structure (there are sub folders) to single folder. Can anyone give me
> some idea like which library files or commands would be suitable for
> this file transfer task. I am sure its matter of few commands. It
> would be even more great if I could get some sample code with
> instructions
>
> Thanks

You should use walk() method from os module and use the copy method from shutil
module. I'm assuming when you say "transfer" you mean "move".

Something like (not tested)

import os
import shutil

# src='source path'
# dst='destination path'

for root, dirs, files in os.walk(src):
for f in files:
srcfile=os.path.join(root, f)
print "move '%s'->'%s'" % (srcfile, dst)
shutil.move(os.path.join(root, f), dst)


-Larry Bates

petercable@gmail.com

2/15/2008 2:06:00 PM

0

On Feb 13, 10:50 pm, Lalit <lalitkris...@gmail.com> wrote:

> I need to write a program which would transfer files under one folder
> structure (there are sub folders) to single folder.

<troll>

find /fromdir -exec mv {} /todir \; -print

</troll>

Pete

Jeff Schwab

2/15/2008 4:29:00 PM

0

petercable@gmail.com wrote:
> On Feb 13, 10:50 pm, Lalit <lalitkris...@gmail.com> wrote:
>
>> I need to write a program which would transfer files under one folder
>> structure (there are sub folders) to single folder.
>
> <troll>
>
> find /fromdir -exec mv {} /todir \; -print
>
> </troll>

-type f

Lalit

2/16/2008 4:03:00 PM

0

Hi this is the code which I wrote till now. It is giving permission
denied error for sub folders of source directory. Does anyone have any
idea what is going wrong

import os
import shutil
def copytreetosinglefolder(src, dst):
names = os.listdir(src)
if (os.path.isdir(dst)==False):
os.mkdir(dst)
for name in names:
srcname = os.path.join(src, name)
try:
shutil.copy2(srcname, dst)
except (IOError, os.error), why:
print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))

copytreetosinglefolder('c:\\src','d:\\dest')

Tim Chase wrote:
>> I am new to python. Infact started yesterday and feeling out of place.
>> I need to write a program which would transfer files under one folder
>> structure (there are sub folders) to single folder. Can anyone give me
>> some idea like which library files or commands would be suitable for
>> this file transfer task. I am sure its matter of few commands. It
>> would be even more great if I could get some sample code with
>
> The shutils.copytree() command[1] will do what you describe
>
> Depending on your source, and if you want to make the dest writeable
> (such as copying off a CD), you may also need to use os.walk() [2]
> combined with os.chmod(filepath, stat.S_IWRITE) [3]
>
> -tkc
>
> [1]
> http://docs.python.org/lib/module-shutil.htm...
>
> [2]
> http://docs.python.org/lib/os-file-dir.htm...
>
> [3]
> http://docs.python.org/lib/os-file-dir.htm...

Diez B. Roggisch

2/16/2008 5:21:00 PM

0

Lalit Krishna schrieb:
> Hi this is the code which I wrote till now. It is giving permission
> denied error for sub folders of source directory. Does anyone have any
> idea what is going wrong
>
> import os
> import shutil
> def copytreetosinglefolder(src, dst):
> names = os.listdir(src)
> if (os.path.isdir(dst)==False):
> os.mkdir(dst)
> for name in names:
> srcname = os.path.join(src, name)
> try:
> shutil.copy2(srcname, dst)
> except (IOError, os.error), why:
> print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))
>
> copytreetosinglefolder('c:\\src','d:\\dest')

Please use a mailer/news-agent that preserves whitespace on the
beginning of the line, and make sure you don't use tabs but spaces to
indent.

Apart from that - why don't you use shutil.copytree? Regarding the error
- are you allowed to copy, can you do it using the shell?

Diez

petercable@gmail.com

2/16/2008 6:25:00 PM

0

On Feb 16, 6:21 pm, "Diez B. Roggisch" <de...@nospam.web.de> wrote:
>
> Please use a mailer/news-agent that preserves whitespace on the
> beginning of the line, and make sure you don't use tabs but spaces to
> indent.
>
> Apart from that - why don't you use shutil.copytree? Regarding the error
> - are you allowed to copy, can you do it using the shell?
>
> Diez

OP stated requirements were to move all the files into a single
folder. Copytree will preserve the directory structure from the source
side of the copy operation.

Lalit Krishna schrieb:

> Hi this is the code which I wrote till now. It is giving permission
> denied error for sub folders of source directory. Does anyone have any
> idea what is going wrong

Python isn't going to let you get around operating system access
controls. Check the permissions on the source directories...

Pete

Tim Chase

2/16/2008 9:14:00 PM

0

> OP stated requirements were to move all the files into a single
> folder. Copytree will preserve the directory structure from the source
> side of the copy operation.

well, it would be "copying [not moving] files through Python",
but if the desire is to flatten the tree into a single directory,
that's also easy enough:

import os, shutil
source_dir = '/home/username/source/'
dest_dir = '/home/username/dest/'
for path, dirs, files in os.walk(source_dir):
for fname in files:
shutil.copy(os.path.join(path, fname), dest_dir)

The above doesn't do any sort of collision-testing, so if more
than one file in source_dir has the same name, the last one to be
copied wins, and the first one gets obliterated. Caveat Coder.

-tkc