471,863 Members | 1,172 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,863 software developers and data experts.

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

Expand|Select|Wrap|Line Numbers
  1. import shutil 
  2. shutil.copyfile(srcfile, dstfile) # copy data only
Jan 27 '09 #1
4 2492
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
  4. print sys.argv
  5. print sys.argv[1]
Expand|Select|Wrap|Line Numbers
  1. ['arg.py', 'argument1']
  2. argument1
Jan 27 '09 #2

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] )
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
What u should do is modify the line
Expand|Select|Wrap|Line Numbers
  1. shutil.copytree(sys.argv[0], sys.argv[1] )
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
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 YellowAndGreen | last post: by
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.