Skip to content

Update this tutorial to Python 3 #4

@Pharaoh00

Description

@Pharaoh00

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions