473,804 Members | 3,021 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Do anyone already have code to copy nested files to a flat directory?

Hi,

I am trying to copy all files under a directory tree to a single
directory (in Windows). I can well write the script myself, but was
wonder if anyone has done it so I won't be reinventing the wheel.

Thanks in advance.

Requirement:

# Source file set
source\file1.tx t
source\file2.do c
source\dir1\fil e3.ini
source\dir2\fil e4.exe

# Desired target file set
target\file1.tx t
target\file2.do c
target\file3.in i
target\file4.ex e

Mar 21 '06 #1
4 1971
OK, please don't bother. Here is my wheel :-)

import os
import shutil

def CopyFlat(source , target):
count = 0
for root, dirs, files in os.walk(source) :
for file in files:
count += 1
name = root + '\\' + file
print '%04d Copying' % count, name,
if os.access(targe t + '\\' + file, os.F_OK):
print 'Already exist and replaced!'
else:
print
shutil.copy(nam e, target)

print '%d files copied' % count

Mar 21 '06 #2
Em Seg, 2006-03-20 Ã*s 18:05 -0800, Podi escreveu:
OK, please don't bother. Here is my wheel :-)


Please, please! Remove all the...

x + "\\" + y

....and put some...

os.path.join(x, y)

.... Please? Even if you're planning to use it only on Windows, please
use standard methods.

Just my 2 cents, though, nothing really special,

--
Felipe.

Mar 21 '06 #3
Podi wrote:
OK, please don't bother. Here is my wheel :-)

import os
import shutil

def CopyFlat(source , target):
count = 0
for root, dirs, files in os.walk(source) :
for file in files:
count += 1
name = root + '\\' + file


A small improvement to the reinvented wheel: use os.sep instead of "\\",
for portability. Better yet, use os.path.join() and avoid the manual
joining of path parts.

-Peter

Mar 21 '06 #4
Thanks for all the suggestions. Now the wheel comes with bells and
whistles ;)

Mar 21 '06 #5

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

Similar topics

6
1861
by: Skip Montanaro | last post by:
I wrote PEP 304, "Controlling Generation of Bytecode Files": http://www.python.org/peps/pep-0304.html quite awhile ago. The first version appeared in January 2003 in response to questions from people about controlling/suppressing bytecode generation in certain situations. It sat idle for a long while, though from time-to-time people would ask about the functionality and I'd respond or update the PEP. In response to another recent...
5
7423
by: Clifford W. Racz | last post by:
Has anyone solved the issue of translating lists in Word 2003 (WordML) into xHTML? I have been trying to get the nested table code for my XSLT to work for a while now, with no way to get the collection that I need. To begin, I am using xsltproc that conmes with Cygwin as my processor. I have no particular affinity to this processor except that it is open source and standards compliant. I don't like M$, but if using a M$ processing...
1
1978
by: LenS | last post by:
If this is the wrong place to post this, please advise better place. Otherwise, I have created the following python program and it works. Running on XP. I think I am now at that stage of learning python where I'm not quit a newbie and I am not really knowlegable. I know just enough to be dangerous and can really screw things up. Any suggestion on improving would be greatly appreciated. However my real question is I would like to run...
6
2202
by: Peter Row | last post by:
Hi, Can someone give me a set of steps for an ASP.NET project (well actually its just a VB.NET class DLL that implements HttpHandler) that will work when moved to another developers machine? Here are some specifics: 1) The code, project/solution files are in MS Visual Source Safe
0
1111
by: Stefan L | last post by:
Hi everybody! I was just starting to use partial classes in my projects, but it is kind of anoying that the VS solution explorer displays the files flat in the hierachy rather than nested. Example: I have a file (and class) Foo.cs that I want to split in a main part and a part, where extrenal interfaces are implemented (maybe Foo.interfaces.cs). How can I tell VS to display Foo.interfaces.cs nested under Foo.cs (like the *.Designer.cs...
4
3232
by: ChrisFrohlich | last post by:
I am trying to deploy two copies of an application to a single website using two virtual directories: "2007" (Production) and "Demo". Each of these applications should point at a different DB, so my thought was to leave everything the same, make two copies of the application files, and just edit the Web.Config file in each copy of the application appropriately. When I try to bring up the Demo site however, I get the following error. ...
232
13383
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
1
4156
by: dkmarni | last post by:
Hi, I am trying to do this perl script, but not able to complete it successfully. Here is the description what the script has to do.. Accept two and only two command line arguments. Again, these will be the names of two directories. This time, make sure that the "sending" directory exists (and is, indeed, a directory). Make sure that the "receiving" directory exists, and if not, create it. Then, copy files from the sending directory to...
3
3930
by: | last post by:
I'm seeking (probably basic) guidance on the right way to split a large site that's supposed to represent one domain(mydomain.org) into many small VS.NET projects, and how to avoid issues with multiple web.config files leading to the error: "It is an error to use a section registered as allowDefinition = 'MachineToApplication'"... I'm fairly new to VS.NET and my sloppy first solution was to make one huge solution/project with just one...
0
9706
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
9584
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10337
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...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
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...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3822
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.