copying files through Python

Discussion in 'Python' started by Lalit, Feb 13, 2008.

  1. Lalit

    Lalit Guest

    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
     
    Lalit, Feb 13, 2008
    #1
    1. Advertising

  2. Lalit

    Tim Chase Guest

    > 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.copytree() 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(filepath, 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
     
    Tim Chase, Feb 13, 2008
    #2
    1. Advertising

  3. Lalit

    Larry Bates Guest

    Lalit 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
    > instructions
    >
    > Thanks


    You should use walk() method from os module and use the copy method from shutil
    module. I'm assuming when you say "transfer" you mean "move".

    Something like (not tested)

    import os
    import shutil

    # src='source path'
    # dst='destination path'

    for root, dirs, files in os.walk(src):
    for f in files:
    srcfile=os.path.join(root, f)
    print "move '%s'->'%s'" % (srcfile, dst)
    shutil.move(os.path.join(root, f), dst)


    -Larry Bates
     
    Larry Bates, Feb 13, 2008
    #3
  4. Lalit

    Guest

    On Feb 13, 10:50 pm, Lalit <> wrote:

    > 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, 2008
    #4
  5. Lalit

    Jeff Schwab Guest

    wrote:
    > On Feb 13, 10:50 pm, Lalit <> wrote:
    >
    >> 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
     
    Jeff Schwab, Feb 15, 2008
    #5
  6. 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 copytreetosinglefolder(src, dst):
    names = os.listdir(src)
    if (os.path.isdir(dst)==False):
    os.mkdir(dst)
    for name in names:
    srcname = os.path.join(src, name)
    try:
    shutil.copy2(srcname, dst)
    except (IOError, os.error), why:
    print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))

    copytreetosinglefolder('c:\\src','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.copytree() 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(filepath, 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
     
    Lalit Krishna, Feb 16, 2008
    #6
  7. 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 copytreetosinglefolder(src, dst):
    > names = os.listdir(src)
    > if (os.path.isdir(dst)==False):
    > os.mkdir(dst)
    > for name in names:
    > srcname = os.path.join(src, name)
    > try:
    > shutil.copy2(srcname, dst)
    > except (IOError, os.error), why:
    > print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))
    >
    > copytreetosinglefolder('c:\\src','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
     
    Diez B. Roggisch, Feb 16, 2008
    #7
  8. Lalit

    Guest

    On Feb 16, 6:21 pm, "Diez B. Roggisch" <> wrote:
    >
    > 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, 2008
    #8
  9. Lalit

    Tim Chase Guest

    > 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.


    well, it would be "copying [not moving] files through Python",
    but if the desire is to flatten the tree into a single directory,
    that's also easy enough:

    import os, shutil
    source_dir = '/home/username/source/'
    dest_dir = '/home/username/dest/'
    for path, dirs, files in os.walk(source_dir):
    for fname in files:
    shutil.copy(os.path.join(path, fname), dest_dir)

    The above doesn't do any sort of collision-testing, so if more
    than one file in source_dir has the same name, the last one to be
    copied wins, and the first one gets obliterated. Caveat Coder.

    -tkc
     
    Tim Chase, Feb 16, 2008
    #9
    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. Replies:
    0
    Views:
    495
  2. Russell Lear

    Copying files in Python

    Russell Lear, Jul 3, 2003, in forum: Python
    Replies:
    0
    Views:
    360
    Russell Lear
    Jul 3, 2003
  3. gtb
    Replies:
    7
    Views:
    393
  4. Catherine Moroney

    fast copying of large files in python

    Catherine Moroney, Nov 2, 2011, in forum: Python
    Replies:
    1
    Views:
    886
    Dave Angel
    Nov 2, 2011
  5. Shawn Mcclain
    Replies:
    0
    Views:
    185
    Shawn Mcclain
    Sep 28, 2007
Loading...

Share This Page