copying files through Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lalit

    copying files through Python

    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
  • Tim Chase

    #2
    Re: copying files through Python

    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.copytre e() 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(filepa th, stat.S_IWRITE) [3]

    -tkc

    [1]
    Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal...


    [2]


    [3]

    Comment

    • petercable@gmail.com

      #3
      Re: copying files through Python

      On Feb 13, 10:50 pm, Lalit <lalitkris...@g mail.comwrote:
      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

      Comment

      • Jeff Schwab

        #4
        Re: copying files through Python

        petercable@gmai l.com wrote:
        On Feb 13, 10:50 pm, Lalit <lalitkris...@g mail.comwrote:
        >
        >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

        Comment

        • Lalit Krishna

          #5
          Re: copying files through Python

          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 copytreetosingl efolder(src, dst):
          names = os.listdir(src)
          if (os.path.isdir( dst)==False):
          os.mkdir(dst)
          for name in names:
          srcname = os.path.join(sr c, name)
          try:
          shutil.copy2(sr cname, dst)
          except (IOError, os.error), why:
          print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))

          copytreetosingl efolder('c:\\sr c','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.copytre e() 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(filepa th, stat.S_IWRITE) [3]
          >
          -tkc
          >
          [1]
          Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal...

          >
          [2]

          >
          [3]
          http://docs.python.org/lib/os-file-dir.html#l2h-2677

          Comment

          • Diez B. Roggisch

            #6
            Re: copying files through Python

            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 copytreetosingl efolder(src, dst):
            names = os.listdir(src)
            if (os.path.isdir( dst)==False):
            os.mkdir(dst)
            for name in names:
            srcname = os.path.join(sr c, name)
            try:
            shutil.copy2(sr cname, dst)
            except (IOError, os.error), why:
            print "Can't copy %s to %s: %s" % (`srcname`, `dst`, str(why))
            >
            copytreetosingl efolder('c:\\sr c','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

            Comment

            • petercable@gmail.com

              #7
              Re: copying files through Python

              On Feb 16, 6:21 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
              >
              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

              Comment

              Working...