Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Do anyone already have code to copy nested files to a flat directory?

Reply
Thread Tools

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

 
 
Podi
Guest
Posts: n/a
 
      03-21-2006
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.txt
source\file2.doc
source\dir1\file3.ini
source\dir2\file4.exe

# Desired target file set
target\file1.txt
target\file2.doc
target\file3.ini
target\file4.exe

 
Reply With Quote
 
 
 
 
Podi
Guest
Posts: n/a
 
      03-21-2006
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(target + '\\' + file, os.F_OK):
print 'Already exist and replaced!'
else:
print
shutil.copy(name, target)

print '%d files copied' % count

 
Reply With Quote
 
 
 
 
Felipe Almeida Lessa
Guest
Posts: n/a
 
      03-21-2006
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.

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      03-21-2006
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

 
Reply With Quote
 
Podi
Guest
Posts: n/a
 
      03-21-2006
Thanks for all the suggestions. Now the wheel comes with bells and
whistles

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why 'Flat is better than nested' Terry Reedy Python 0 07-31-2012 09:04 PM
Why "flat is better than nested"? kj Python 53 11-10-2010 05:08 AM
Flat HTML headers to nested XML sections CrazyAtlantaGuy XML 3 05-22-2007 07:13 PM
Formac XTreme 2010 20" flat panel TFT - anyone have this? Symphony Photography Digital Photography 0 10-23-2004 05:14 AM
I have created a website "lookup" i dont think its gonna get bigger and fancyer than it already is but have a look Rahmi Acar C++ 0 04-20-2004 10:18 PM



Advertisments