Last active
May 22, 2018 23:07
-
-
Save glwinsor/38ee896ff4fb4ecde86e0fc7b93c955f to your computer and use it in GitHub Desktop.
open and read file
This file contains 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
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