Skip to content

Instantly share code, notes, and snippets.

@refo
Created January 10, 2019 11:30
Show Gist options
  • Save refo/dcccadd357bbf2f470c011b056c60e30 to your computer and use it in GitHub Desktop.
Save refo/dcccadd357bbf2f470c011b056c60e30 to your computer and use it in GitHub Desktop.
Basic file reading, writing and argument operations in python
#!/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
@dckutlular
Copy link

Clean and comprehensible. Thank you for sharing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment