Skip to content

Instantly share code, notes, and snippets.

@bryder
Last active August 29, 2015 14:10
Show Gist options
  • Save bryder/d1c981bd60e9657a594c to your computer and use it in GitHub Desktop.
Save bryder/d1c981bd60e9657a594c to your computer and use it in GitHub Desktop.
short ways to do file I/O in python
sys.stderr.write(open("/tmp/filename").read())
cmdline = open("/proc/cmdline").read()
with open("/dev/blah","r") as f:
for l in f: # l in f means read each line
print l
try:
with open(FILENAME, 'w') as f:
f.write(STUFF_TO_WRITE)
except Exception as e:
log.error("Could not write to %s because %s" % str(FILENAME, e))
sys.exit(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment