Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update this tutorial to Python 3 #4

Open
Pharaoh00 opened this issue Dec 16, 2018 · 0 comments
Open

Update this tutorial to Python 3 #4

Pharaoh00 opened this issue Dec 16, 2018 · 0 comments

Comments

@Pharaoh00
Copy link

Pharaoh00 commented Dec 16, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant