0

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?

Learner
  • 3
  • 4
  • you need to add a check if cmdline arguments are passed if not use raw_input or input to ask for the same – avasal Jan 15 '13 at 09:30
  • in raw_input or input i have to write correct path. but i want to select the folders by pressing tab @avasal – Learner Jan 15 '13 at 09:32
  • For make things clear, what you want is to ask the user to enter the path, but not to force him to write it completely by hand, helping him with the terminal-completion by pressing TAB? – Cynical Jan 15 '13 at 10:03

4 Answers4

2

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

Jakob Bowyer
  • 33,878
  • 8
  • 76
  • 91
0

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
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
0

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.

Community
  • 1
  • 1
Cynical
  • 9,328
  • 1
  • 15
  • 30
0

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
NlightNFotis
  • 9,559
  • 5
  • 43
  • 66