By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,923 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,923 IT Pros & Developers. It's quick & easy.

shutil module (directory input from terminal)

P: 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 sourcefile, destinationfile

Expand|Select|Wrap|Line Numbers
  1. import shutil 
  2. shutil.copyfile(srcfile, dstfile) # copy data only
Jan 27 '09 #1
Share this Question
Share on Google+
4 Replies

Expert Mod 2.5K+
P: 2,851
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 argument1
Expand|Select|Wrap|Line Numbers
  1. #
  2. import sys
  4. print sys.argv
  5. print sys.argv[1]
Expand|Select|Wrap|Line Numbers
  1. ['', 'argument1']
  2. argument1
Jan 27 '09 #2

P: 14

hey man, thanks for replay

i did the following

Expand|Select|Wrap|Line Numbers
  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 '/home/waseem/My Pictures/yemen 2008/2008/1' '/home/waseem/h'
Traceback (most recent call last):
File "", line 4, in <module>
shutil.copytree(sys.argv[0], sys.argv[1] )
File "/usr/lib/python2.5/", line 109, in copytree
names = os.listdir(src)
OSError: [Errno 20] Not a directory: ''
Jan 28 '09 #3

P: 107
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

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