Reputation: 3
this question may be a foolish question. since im new to this python , please help me in this issue.
if __name__== '__main__' :
path=sys.argv[1]
in the above code im reading arguments from command line. my argument is a path to some file in system. when i dont give argumentts it is raising a error "list index out of range"
instead of reading from command line i want to take input in the following manner
" enter path to your file.."
after showing this line how can i parse through the file system by pressing tab and take path as input?
Upvotes: 0
Views: 1278
Reputation: 9805
Something somehow less relevant:
If you would like to perform more sophisticated command line options parsing, consider using the argparse
module.
This is a simple demonstration of the module in a script I made:
import argparse
parser = argparse.ArgumentParser(description='MD5 Cracker')
parser.add_argument('target', metavar='Target_MD5_Hash', help='The target MD5 that you want to have cracked.')
parser.add_argument('--online', '-o', help='MD5 Cracker will try to crack the password using online MD5 cracking websites and databases', default=False)
parser.add_argument('--list', '-l', help='MD5 Cracker will try to crack the passwork offline with a dictionary attack using the list supplied', default=False)
parser.add_argument('--interactive', '-i', help='MD5 Cracker will enter interactive mode, allowing you to check passwords without reinitiating the software each time', default=False)
if __name__ == '__main__':
cli_args = parser.parse_args()
args_dict = cli_args.__dict__ # here it is cast into a dictionary to allow for easier manipulation of contents
Upvotes: 0
Reputation: 35950
Try this:
path = raw_input("enter path to your file..")
print path
If you are using commandline arguments like hello.py myfile.txt
, then use
if len(sys.argv) > 1:
path = sys.argv[1]
Update:
If you need to show to the user all the files in a directory. Use this:
import os
i = 1
for item in os.listdir("F:/python/Lib"):
if os.path.isfile(os.path.join("F:/python/Lib", item)):
print str(i) + " : " + item
i += 1
Output:
>>>
1 : abc.py
2 : abc.pyc
3 : aifc.py
4 : antigravity.py
5 : anydbm.py
6 : argparse.py
7 : ast.py
8 : asynchat.py
9 : asyncore.py
10 : atexit.py
11 : atexit.pyc
12 : audiodev.py
13 : base64.py
14 : base64.pyc
15 : BaseHTTPServer.py
16 : BaseHTTPServer.pyc
17 : Bastion.py
18 : bdb.py
19 : bdb.pyc
Upvotes: 0
Reputation: 9558
how can i parse through the file system by pressing tab and take path as input
This is not an easy task, since your pressing tab implies the completion, which is done at bash
or batch
level (the terminal on which you are running your python program).
You might need to add a function which calls the appropriate subroutines from the os
and sys
module, and do a custom completion.
Here you can find an idea of what I mean, hope it helps.
Upvotes: 0
Reputation: 34688
sys.argv
is simply a list, by saying sys.argv[1]
its the same as saying args = [0]; args[1]
You need to check that index 1
actually exists
Upvotes: 2