Skip to content

Instantly share code, notes, and snippets.

@glwinsor
Last active May 22, 2018 23:07
Show Gist options
  • Save glwinsor/38ee896ff4fb4ecde86e0fc7b93c955f to your computer and use it in GitHub Desktop.
Save glwinsor/38ee896ff4fb4ecde86e0fc7b93c955f to your computer and use it in GitHub Desktop.
open and read file
import sys
from optparse import OptionParser
try:
# assumes file was specified as argument in the command line using optparse with --file option
file=open(options.file, 'r')
except FileNotFoundError: # works for python3
print("Could not find file")
sys.exit()
except IOError:
print("Could not open file")
sys.exit()
# iterate over the file
for line in file:
print(line.rstrip())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment