Created
January 10, 2019 11:30
-
-
Save refo/dcccadd357bbf2f470c011b056c60e30 to your computer and use it in GitHub Desktop.
Basic file reading, writing and argument operations in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import sys, os | |
fileName = sys.argv[1] if len(sys.argv) > 1 else "default" | |
cwd = os.getcwd() | |
path = os.path.join(cwd, "{}.txt".format(fileName)) | |
print "File:" | |
print path | |
print "" | |
if os.path.isfile(path): | |
file = open(path, 'r') | |
contents = file.read() | |
else: | |
file = open(path, 'w') | |
contents = 'Hebelek ?' | |
file.write(contents) | |
print "New file created" | |
file.close() | |
print "File contents" | |
print contents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Clean and comprehensible. Thank you for sharing.