-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_objects.py
50 lines (42 loc) · 1.45 KB
/
file_objects.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
""" File Objects
Use Python's file class to store/retrieve data to/from a text file
Allows the user to enter numbers from the keyboard and save them to a file
Also allows retrieval of the data prevously saved to file
User can specify the name of the file and thus works with multiple files
"""
def load_data(filename):
""" (file) -> int
Print the elements stored in the text file <filename>
"""
with open(filename) as f_obj: # Open file to read & assign file object
for line in f_obj: # Read each line as text
print(int(line)) # Convert to int & display
def store_data(filename):
"""
Store data to <filename> text file
"""
with open(filename, 'w') as f_obj:
number = 0
while number != 999:
number = int(input('Pls enter a number (999 quits): '))
if number != 999:
f_obj.write(str(number) + '\n')
else:
break # Exit loop
def work_with_file():
"""
Interactive function that allows the user to create or
consume files of numbers
"""
done = False # Init
while not done:
cmd = input('S)ave L)oad Q)uit: ')
if cmd.upper() == 'S':
store_data(input('Enter file name: '))
elif cmd.upper() == 'L':
load_data(input('Enter file name: '))
elif cmd.upper() == 'Q':
done = True
if __name__ == '__main__':
work_with_file()