469,623 Members | 1,545 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,623 developers. It's quick & easy.

shutil module (directory input from terminal)

14
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

Expand|Select|Wrap|Line Numbers
  1. import shutil 
  2. shutil.copyfile(srcfile, dstfile) # copy data only
thanks
Jan 27 '09 #1
4 2418
bvdet
2,851 Expert Mod 2GB
The arguments passed to the script on the command line are available in list object sys.argv. Example at command prompt:
Expand|Select|Wrap|Line Numbers
  1. python arg.py argument1
Expand|Select|Wrap|Line Numbers
  1. # arg.py
  2. import sys
  3.  
  4. print sys.argv
  5. print sys.argv[1]
Output:
Expand|Select|Wrap|Line Numbers
  1. ['arg.py', 'argument1']
  2. argument1
Jan 27 '09 #2
klia
14
@bvdet

hey man, thanks for replay

i did the following

Expand|Select|Wrap|Line Numbers
  1. #arg.py
  2. import sys
  3. import shutil
  4. shutil.copytree(sys.argv[0], sys.argv[1] )
  5.  
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'
Jan 28 '09 #3
@klia
What u should do is modify the line
Expand|Select|Wrap|Line Numbers
  1. shutil.copytree(sys.argv[0], sys.argv[1] )
to
Expand|Select|Wrap|Line Numbers
  1. 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.
Jan 28 '09 #4
klia
14
Thank you very much.

it worked perfectly
Jan 28 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Peter Åstrand | last post: by
6 posts views Thread by Daniel Bickett | last post: by
6 posts views Thread by Antoine De Groote | last post: by
8 posts views Thread by David Nicolson | last post: by
10 posts views Thread by yinglcs | last post: by
10 posts views Thread by David C. Ullrich | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.