I’m trying open a txt file and read it, but I’m getting a type error and I’m unsure why. If you you could please provide a reasoning along with the correct syntax, I’m trying to get a better grasp of what’s going on underneath. Here’s the code, it’s pretty simple I think:
from sys import argv
script = argv
filename = argv
txt = open(filename)
print "Here's your file %r" %filename
print txt.read()
Muchas Gracias
argvis a list, not a string. You wantConsider using
argparseinstead of handingsys.argvdirectly:It looks much more complicated, but it will make your command-line utility much more flexible, easier to expand, and it will ensure you get proper documentation on the command line.