views:

75

answers:

2

i am running this:

import csv
import sys
reader = csv.reader(open(sys.argv[0], "rb"))
for row in reader:
    print row

and i get this in response:

['import csv']
['import sys']
['reader = csv.reader(open(sys.argv[0]', ' "rb"))']
['for row in reader:']
['    print row']
>>> 

for the sys.argv[0] i would like it to prompt me to enter a filename.

how do i get it to prompt me to enter a filename?

+7  A: 

Use the raw_input() function to get input from users:

print "Enter a file name:",
filename = raw_input()

or just:

filename = raw_input('Enter a file name: ')
Dave Webb
Or `raw_input("Enter a file name: ")`
Longpoke
@Longpoke - Good point; have updated the answer.
Dave Webb
In Python 3, it's just `input()` - __caution__ : there is an input method in 2.x too, but it eval()s the input and is therefore __evil__.
delnan
+4  A: 

sys.argv[0] is not the first argument but the filename of the python program you are currently executing. I think you want sys.argv[1]

Nikolaus Gradwohl