Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting user input [duplicate]

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?

like image 606
Alex Gordon Avatar asked Jul 27 '10 15:07

Alex Gordon


2 Answers

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

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

or just:

filename = raw_input('Enter a file name: ')

or if in Python 3.x:

filename = input('Enter a file name: ')
like image 126
Dave Webb Avatar answered Oct 14 '22 18:10

Dave Webb


In python 3.x, use input() instead of raw_input()

like image 118
Sunny Tambi Avatar answered Oct 14 '22 17:10

Sunny Tambi