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

Discussion in 'Python' started by Podi, Mar 21, 2006.

  1. Podi

    Podi Guest

    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
    Podi, Mar 21, 2006
    #1
    1. Advertising

  2. Podi

    Podi Guest

    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
    Podi, Mar 21, 2006
    #2
    1. Advertising

  3. Re: Do anyone already have code to copy nested files to a flatdirectory?

    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.
    Felipe Almeida Lessa, Mar 21, 2006
    #3
  4. Podi

    Peter Hansen Guest

    Re: Do anyone already have code to copy nested files to a flatdirectory?

    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
    Peter Hansen, Mar 21, 2006
    #4
  5. Podi

    Podi Guest

    Thanks for all the suggestions. Now the wheel comes with bells and
    whistles ;)
    Podi, Mar 21, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homer
    Replies:
    230
    Views:
    10,699
    Roedy Green
    May 1, 2006
  2. CrazyAtlantaGuy

    Flat HTML headers to nested XML sections

    CrazyAtlantaGuy, May 16, 2007, in forum: XML
    Replies:
    3
    Views:
    651
    CrazyAtlantaGuy
    May 22, 2007
  3. kj
    Replies:
    53
    Views:
    2,458
    alex23
    Nov 10, 2010
  4. Terry Reedy

    Why 'Flat is better than nested'

    Terry Reedy, Jul 31, 2012, in forum: Python
    Replies:
    0
    Views:
    150
    Terry Reedy
    Jul 31, 2012
  5. Ian Kelly

    Re: Why 'Flat is better than nested'

    Ian Kelly, Jul 31, 2012, in forum: Python
    Replies:
    0
    Views:
    142
    Ian Kelly
    Jul 31, 2012
Loading...

Share This Page