-
Notifications
You must be signed in to change notification settings - Fork 115
Open
Description
Hey, nice github tutorial for python... But all your code is on Python 2!
Can i start update to python 3? And make some changes, here is one example:
# from sys module import a member called 'argv'
from sys import argv
#unpack
script, filename = argv
fp = open(filename, "w")
print "Writing to file %r " % fp
fp.write("Hello World")
fp.close()
This example is from python_write.py, but here is the problem, when you are working with files if you open you need to close it.
The way to do it more "easily" is yo use context manager with statement.
So:
# from sys module import a member called 'argv'
from sys import argv
#unpack
script, filename = argv
with open(filename, "w") as f:
print("Writing to file".format(f)) # As python 3+
f.write("Hello World")
More clean, and modern python.
Metadata
Metadata
Assignees
Labels
No labels