473,657 Members | 2,801 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2065
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.copytre e() 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(filepa th, 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...@g mail.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********@gmai l.com wrote:
On Feb 13, 10:50 pm, Lalit <lalitkris...@g mail.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 copytreetosingl efolder(src, dst):
names = os.listdir(src)
if (os.path.isdir( dst)==False):
os.mkdir(dst)
for name in names:
srcname = os.path.join(sr c, name)
try:
shutil.copy2(sr cname, dst)
except (IOError, os.error), why:
print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))

copytreetosingl efolder('c:\\sr c','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.copytre e() 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(filepa th, 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 copytreetosingl efolder(src, dst):
names = os.listdir(src)
if (os.path.isdir( dst)==False):
os.mkdir(dst)
for name in names:
srcname = os.path.join(sr c, name)
try:
shutil.copy2(sr cname, dst)
except (IOError, os.error), why:
print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))

copytreetosingl efolder('c:\\sr c','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.w eb.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
671
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 if it must be file to file)? Perserving file creation time is important. Platform independence also nice. Clearly I could open the source file for reading and a destination file for writing and copy the bits over, but that might not preserve...
3
3640
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 my settings if my profile became tampered with or corrupt. Is there any sample code available out there? -Robert
4
2973
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 http://alexvn.freeservers.com/s1/perfometer.html
0
1542
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 checks the .NET CF version. The first message I receive is: Files in the package 'netcf.all.wce4.armv4.cab' are more recent on the device than on the development computer. Consider upgrading to the latest version. Files affected are:
1
2342
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 impossible to determine if a file, once added to an archive, will cause the archive size to exceed the maximum size. I believe that to do this properly, you need to copy the state of tar file (basically the current file offset as well as the state of...
2
8127
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 problem I am facing is that the copy statement I am using is that when overwriting the files it will prompt for each file, so I can not find an option to tell it to overwrite all. If strCopyToFolder.EndsWith("\") = False Then strCopyToFolder +=...
7
2189
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 can be one to three digits. Example directory: other.zip
6
4971
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, or i couldn't find sth which give totalbytes/written bytes class. And does system.io.file class provide awaring of the chunks / bytes of the files bytes which are written?
13
10659
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 process the file. The appearance of the command file triggers the device to grab the original file. My problem is I don't want to write the command file to the device until the original file from the workflow has been copied completely. Since these...
0
8403
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8737
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5636
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2735
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.