-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab05B-Solutions.py
More file actions
44 lines (31 loc) · 1.03 KB
/
Lab05B-Solutions.py
File metadata and controls
44 lines (31 loc) · 1.03 KB
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
35
36
37
38
39
40
41
42
43
44
# Lab05B
import os
# We use relative paths in this code.
# Before getting going let's just confirm the current working directory
print(os.getcwd())
print("All lines in the file\n")
# Old style. open and manual close
file = open(r".\employees.txt")
for line in file:
print(line.strip())
file.close()
print("\nJust the unique ones\n")
# Display only unique items
# Modern style. with automates the close
with open(r".\employees.txt") as file:
import pathlib
my_file = pathlib.Path(r".\unique_employees.txt")
if my_file.is_file():
print("I am about to nuke your file")
with open(r".\unique_employees.txt", "w") as output_file:
emp_nos = set()
for line in file:
if not line in emp_nos:
emp_nos.add(line)
print(line.strip())
output_file.write(line)
# Demonstrate picking up values from the command line
# Such values can be used as file names, switches, etc
# (and it's probably best to use a library to handle them)
import sys
print(sys.argv)