Reputation: 193
I have a very simple python script that should print the contents of a file that is passed like this: python script.py stuff.txt
. I don't get any output.
Here is the code:
import sys
fname = sys.argv[1]
f = open(fname, 'r')
f.read()
From what I have read, this is supposed to work. Why not?
Upvotes: 0
Views: 206
Reputation: 13542
You read the file, but you don't do anything with the data.
print(f.read())
Or, for better style:
import sys
fname = sys.argv[1]
with open(fname, 'r') as f:
print(f.read())
This is the recommended way to use files. It guarantees the file is closed when you exit the with
block. Does not really matter for your small script, but it's a good habit to take.
Upvotes: 6