473,405 Members | 2,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

copying files through Python

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
Feb 13 '08 #1
6 2051
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
Feb 13 '08 #2
On Feb 13, 10:50*pm, Lalit <lalitkris...@gmail.comwrote:
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
Feb 15 '08 #3
pe********@gmail.com wrote:
On Feb 13, 10:50 pm, Lalit <lalitkris...@gmail.comwrote:
>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
Feb 15 '08 #4
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.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
Feb 16 '08 #5
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
Feb 16 '08 #6
On Feb 16, 6:21*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:
>
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
Feb 16 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Russell Lear | last post by:
A simple question from a python beginner: How do I do 'cp -p source destination' where source could be a list of files and destination could be a file or directory (I could break the operation up...
3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
4
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
0
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and...
1
by: chris.atlee | last post by:
I'm writing a program in python that creates tar files of a certain maximum size (to fit onto CD/DVD). One of the problems I'm running into is that when using compression, it's pretty much...
2
by: Edhy Rijo [Progytech] | last post by:
Hi All, I am learning VB.NET and building small application that will do the following: 1.. Copy all files from a DVD to a specific folder in the hard drive and overwrite all files always. The...
7
by: gtb | last post by:
I wish to copy the highest version number of a file from directory \ \ \fileserver\D:\scripts to C:\scripts where the file names are of the form filename_MM.NN.SS.zip, where MM, NN, and SS...
6
by: kimiraikkonen | last post by:
Hi, I use system.io.file class to copy files but i have a difficulty about implementing a basic / XP-like progress bar indicator during copying process. My code is this with no progress bar,...
13
by: writeson | last post by:
Hi all, I'm writing some code that monitors a directory for the appearance of files from a workflow. When those files appear I write a command file to a device that tells the device how to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.