shutil module (directory input from terminal)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • klia
    New Member
    • Oct 2008
    • 14

    shutil module (directory input from terminal)

    hello folks

    i am trying to tweak the current codes so that later when i call it from the terminal i can provide sourcefile and the destination file rather being fixed in the code. because now i have to specify the sourcefile and the destinationfile in codes and not left to be specified from the terminal. i want to be able to do this.

    python shutil_copy.py sourcefile, destinationfile

    Code:
    import shutil 
    shutil.copyfile(srcfile, dstfile) # copy data only
    thanks
    Last edited by bvdet; Jan 27 '09, 05:01 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The arguments passed to the script on the command line are available in list object sys.argv. Example at command prompt:
    Code:
    python arg.py argument1
    Code:
    # arg.py
    import sys
    
    print sys.argv
    print sys.argv[1]
    Output:
    Code:
    ['arg.py', 'argument1']
    argument1

    Comment

    • klia
      New Member
      • Oct 2008
      • 14

      #3
      Originally posted by bvdet
      The arguments passed to the script on the command line are available in list object sys.argv. Example at command prompt:
      Code:
      python arg.py argument1
      Code:
      # arg.py
      import sys
      
      print sys.argv
      print sys.argv[1]
      Output:
      Code:
      ['arg.py', 'argument1']
      argument1

      hey man, thanks for replay

      i did the following

      Code:
      #arg.py
      import sys
      import shutil
      shutil.copytree(sys.argv[0], sys.argv[1] )
      and i got the following error, for the record i am trying to copy files from one directory to another one

      waseem@Linux:~/Project2$ python copying_photos. py '/home/waseem/My Pictures/yemen 2008/2008/1' '/home/waseem/h'
      Traceback (most recent call last):
      File "copying_photos .py", line 4, in <module>
      shutil.copytree (sys.argv[0], sys.argv[1] )
      File "/usr/lib/python2.5/shutil.py", line 109, in copytree
      names = os.listdir(src)
      OSError: [Errno 20] Not a directory: 'copying_photos .py'

      Comment

      • kaarthikeyapreyan
        New Member
        • Apr 2007
        • 106

        #4
        Re:shutil.copyt ree

        Originally posted by klia
        hey man, thanks for replay

        i did the following

        Code:
        #arg.py
        import sys
        import shutil
        shutil.copytree(sys.argv[0], sys.argv[1] )
        and i got the following error, for the record i am trying to copy files from one directory to another one

        waseem@Linux:~/Project2$ python copying_photos. py '/home/waseem/My Pictures/yemen 2008/2008/1' '/home/waseem/h'
        Traceback (most recent call last):
        File "copying_photos .py", line 4, in <module>
        shutil.copytree (sys.argv[0], sys.argv[1] )
        File "/usr/lib/python2.5/shutil.py", line 109, in copytree
        names = os.listdir(src)
        OSError: [Errno 20] Not a directory: 'copying_photos .py'
        What u should do is modify the line
        Code:
        shutil.copytree(sys.argv[0], sys.argv[1] )
        to
        Code:
        shutil.copytree(sys.argv[1], sys.argv[2] )
        Make sure that the source is a directory, an existing one and the destination is a non-existent directory name.

        Comment

        • klia
          New Member
          • Oct 2008
          • 14

          #5
          Thank you very much.

          it worked perfectly

          Comment

          Working...