copying files through Python

L

Lalit

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
 
T

Tim Chase

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.html#l2h-2297

[2]
http://docs.python.org/lib/os-file-dir.html#l2h-2707

[3]
http://docs.python.org/lib/os-file-dir.html#l2h-2677
 
L

Larry Bates

Lalit said:
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
 
P

petercable

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
 
L

Lalit Krishna

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 said:
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.html#l2h-2297

[2]
http://docs.python.org/lib/os-file-dir.html#l2h-2707

[3]
http://docs.python.org/lib/os-file-dir.html#l2h-2677
 
D

Diez B. Roggisch

Lalit said:
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
 
P

petercable

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 said:
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
 
T

Tim Chase

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top