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

P

Podi

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
 
P

Podi

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
 
F

Felipe Almeida Lessa

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,
 
P

Peter Hansen

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

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top