Getting user input


Question

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?

1
137
5/9/2018 11:32:35 AM

Accepted Answer

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

113
3/20/2016 6:04:21 AM

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: ')

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon