/
file_io_with_open.py
187 lines (145 loc) · 3.03 KB
/
file_io_with_open.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
path = 'data/src/test.txt'
f = open(path)
print(type(f))
# <class '_io.TextIOWrapper'>
f.close()
with open(path) as f:
print(type(f))
# <class '_io.TextIOWrapper'>
# with open('data/src/test_error.txt') as f:
# print(type(f))
# FileNotFoundError: [Errno 2] No such file or directory: 'data/src/test_error.txt'
with open(path) as f:
s = f.read()
print(type(s))
print(s)
# <class 'str'>
# line 1
# line 2
# line 3
with open(path) as f:
s = f.read()
print(s)
# line 1
# line 2
# line 3
with open(path) as f:
l = f.readlines()
print(type(l))
print(l)
# <class 'list'>
# ['line 1\n', 'line 2\n', 'line 3']
with open(path) as f:
l_strip = [s.rstrip() for s in f.readlines()]
print(l_strip)
# ['line 1', 'line 2', 'line 3']
with open(path) as f:
for s_line in f:
print(repr(s_line))
# 'line 1\n'
# 'line 2\n'
# 'line 3'
with open(path) as f:
print(repr(next(f)))
print(repr(next(f)))
print(repr(next(f)))
# print(repr(next(f)))
# StopIteration:
# 'line 1\n'
# 'line 2\n'
# 'line 3'
with open(path) as f:
print(repr(f.readline()))
print(repr(f.readline()))
print(repr(f.readline()))
print(repr(f.readline()))
print(repr(f.readline()))
# 'line 1\n'
# 'line 2\n'
# 'line 3'
# ''
# ''
path_w = 'data/temp/test_w.txt'
s = 'New file'
with open(path_w, mode='w') as f:
f.write(s)
with open(path_w) as f:
print(f.read())
# New file
# with open('data/src/new_dir/test_w.txt', mode='w') as f:
# f.write(s)
# FileNotFoundError: [Errno 2] No such file or directory: 'data/src/new_dir/test_w.txt'
s = 'New line 1\nNew line 2\nNew line 3'
with open(path_w, mode='w') as f:
f.write(s)
with open(path_w) as f:
print(f.read())
# New line 1
# New line 2
# New line 3
l = ['One', 'Two', 'Three']
with open(path_w, mode='w') as f:
f.writelines(l)
with open(path_w) as f:
print(f.read())
# OneTwoThree
with open(path_w, mode='w') as f:
f.write('\n'.join(l))
with open(path_w) as f:
print(f.read())
# One
# Two
# Three
# with open(path_w, mode='x') as f:
# f.write(s)
# FileExistsError: [Errno 17] File exists: 'data/src/test_w.txt'
try:
with open(path_w, mode='x') as f:
f.write(s)
except FileExistsError:
pass
import os
if not os.path.isfile(path_w):
with open(path_w, mode='w') as f:
f.write(s)
with open(path_w, mode='a') as f:
f.write('Four')
with open(path_w) as f:
print(f.read())
# One
# Two
# ThreeFour
with open(path_w, mode='a') as f:
f.write('\nFour')
with open(path_w) as f:
print(f.read())
# One
# Two
# ThreeFour
# Four
with open(path_w, mode='r+') as f:
f.write('12345')
with open(path_w) as f:
print(f.read())
# 12345wo
# ThreeFour
# Four
with open(path_w, mode='r+') as f:
f.seek(3)
f.write('---')
with open(path_w) as f:
print(f.read())
# 123---o
# ThreeFour
# Four
with open(path_w) as f:
l = f.readlines()
l.insert(0, 'FIRST\n')
with open(path_w, mode='w') as f:
f.writelines(l)
with open(path_w) as f:
print(f.read())
# FIRST
# 123---o
# ThreeFour
# Four