View Full Version : bash to python

P.ap G
July 20th, 2010, 04:43 PM
Any suggestions on re-writing this function I wrote into python , I'm getting stuck giving `raw_input`
multiple arguments?

July 20th, 2010, 06:50 PM
Easy path:

filename = raw_input("Name of the file:")
permissions = raw_input("Octal Permissions:")

The other way, would be to split, working on all this different inputs:

"Name of the file.txt" 775
Name\ of\ the\ file.txt 775
Nameofthefile.txt 775

But, you can also exploit the command-line arguments, as bash already splits it accordingly, saving you time. For example, I made this little program:

#!/usr/bin/env python

import sys

print sys.argv

Testing it:

:~/Desktop$ python asdf.py "Filename with spaces.txt" 777
['asdf.py', 'Filename with spaces.txt', '777']
:~/Desktop$ python asdf.py Filename\ with\ spaces.txt 777
['asdf.py', 'Filename with spaces.txt', '777']
:~/Desktop$ python asdf.py filenamewithoutspaces.txt 777
['asdf.py', 'filenamewithoutspaces.txt', '777']

Are you rewriting this in python for any particular reason? Learning? Cross-compatibility?