Skip to content

Commit cc106f1

Browse files
committed
Add Working with File section: Reading, Writing, Updating & Pickling data into/ from a file.
1 parent b7b3d11 commit cc106f1

File tree

4 files changed

+119
-2
lines changed

4 files changed

+119
-2
lines changed

dreariness.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
She love me!
2+
Yeah
3+
Yeah
4+
Yeah
5+
She love me!
6+
Yeah
7+
Yeah
8+
Yeah
9+
(by Truong Thinh)

fundamentals/custom_functions.py

+96
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,105 @@
11
# Author: Nguyen Truong Thinh
22
# Contact me: [email protected] || +84393280504
33
from math import sqrt
4+
import pickle, os
45

56

67
# Custom functions
8+
def file_with_pickle_data(file_name):
9+
"""
10+
Pickling data for more efficient to use a machine-readable binary file.
11+
:param file_name: Name of the file
12+
:return: None
13+
"""
14+
if not os.path.isfile(file_name):
15+
data = [0, 1]
16+
data[0] = input('Enter topic: ')
17+
data[1] = input('Enter series: ')
18+
19+
file = open(file_name, 'wb')
20+
pickle.dump(data, file)
21+
file.close()
22+
else:
23+
file = open(file_name, 'rb')
24+
data = pickle.load(file)
25+
file.close()
26+
print('\nWelcome back to: ', data[0], '-', data[1])
27+
28+
29+
def update_a_file_with_block(file_name, mode, new_content, position):
30+
"""
31+
Updating file strings
32+
:param file_name: Name of the file
33+
:param mode: File mode
34+
:param new_content: The new content will be updated
35+
:param position: The current file position
36+
:return: None
37+
"""
38+
with open(file_name, mode) as file:
39+
text = file.read()
40+
print('\nString: ', text)
41+
print('\nPosition in file now: ', file.tell())
42+
file.seek(position)
43+
file.write(new_content)
44+
file.seek(0)
45+
text = file.read()
46+
print('\nString: ', text)
47+
print('\nPosition in file now: ', file.tell())
48+
49+
50+
def write_a_file_with_block(file_name, mode, content):
51+
"""
52+
Writing files: Adds content to the file & display the file's current status
53+
in the "with" block.
54+
:param file_name: Name of the file
55+
:param mode: File mode
56+
:param content: The content will be written
57+
:return: None
58+
"""
59+
with open(file_name, mode) as file:
60+
file.write(content)
61+
print('\nFile Now Closed?: ', file.closed)
62+
print('File Now Closed?: ', file.closed)
63+
64+
65+
def re_write_a_file(file_name):
66+
"""
67+
Re-write a file to append a citation
68+
:param file_name: Name of the file
69+
:return: None
70+
"""
71+
file = open(file_name, 'a')
72+
file.write('(by Truong Thinh)')
73+
file.close()
74+
75+
76+
def read_a_file(file_name, mode):
77+
"""
78+
Reading files: Read the entire contents
79+
:param file_name: Name of the file
80+
:param mode: File mode
81+
:return: None
82+
"""
83+
file = open(file_name, mode)
84+
for line in file:
85+
# Do something here.
86+
print(line, end='')
87+
file.close()
88+
89+
90+
def write_a_file(file_name, mode, content):
91+
"""
92+
Writing files: Adds content to the file
93+
:param file_name: Name of the file
94+
:param mode: File mode
95+
:param content: The content will be written
96+
:return: None
97+
"""
98+
file = open(file_name, mode)
99+
file.write(content)
100+
file.close()
101+
102+
7103
def get_data_from_list(stocks):
8104
"""
9105
Get a list of:

main.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,23 @@
7676
friends_stock_list = [info_dict_dev, info_dict_tester, info_dict_po, info_dict_boss]
7777
data = get_data_from_list(friends_stock_list)
7878
print(data)
79+
# File operations
80+
file_name = 'dreariness.txt'
81+
content = 'She love me!\n'
82+
content += 'Yeah\n'
83+
content += '\tYeah\n'
84+
content += '\t\tYeah\n'
85+
write_a_file(file_name, 'w', content)
86+
read_a_file(file_name, 'r')
87+
update_a_file_with_block(file_name, 'r+', content, 35)
88+
re_write_a_file(file_name)
89+
file_with_pickle_data('pickle.dat')
90+
7991
# Objects
8092
p_x = Point(1, 0)
8193
p_y = Point(5, 3)
82-
# draw(p_x.x, p_x.y)
83-
# draw(p_y.x, p_y.y)
94+
draw(p_x.x, p_x.y)
95+
draw(p_y.x, p_y.y)
8496
print(f'\nDistance between {p_x} & {p_y} is {p_x.distance(p_y)}')
8597
del p_x
8698
del p_y

pickle.dat

87 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)