/
bond.py
34 lines (27 loc) · 995 Bytes
/
bond.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
bond.py
Read a text input file into a list.
Each line in the file will be a separate list item.
"""
import sys
if sys.platform.startswith("darwin"): #macOS Mojave 10.14.6
filename = "/Users/myname/Desktop/bond.txt"
elif sys.platform.startswith("win32"): #Microsoft Windows 7 Home Premium
filename = r"C:\Users\Myname\Desktop\bond.txt"
else:
print(f"Unknown platform {sys.platform}", file = sys.stderr)
sys.exit(1)
try:
infile = open(filename)
except FileNotFoundError:
print(f'Sorry, could not find file "{filename}".', file = sys.stderr)
sys.exit(1)
except PermissionError:
print(f'Sorry, no permission to open file "{filename}".', file = sys.stderr)
sys.exit(1)
lines = infile.readlines() #lines is a list of strings
infile.close()
sortedLines = sorted(lines, key = lambda line: line[5:]) #alphabetical order by movie name
for line, sortedLine in zip(lines, sortedLines):
print(f"{line.rstrip():36} {sortedLine.rstrip()}")
sys.exit(0)