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 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
Share this Question
Share on Google+
4 Replies


bvdet
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 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

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

kaarthikeyapreyan
100+
P: 107
@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

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.