-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path32 - Read, Write & Append.py
More file actions
323 lines (176 loc) · 8.92 KB
/
32 - Read, Write & Append.py
File metadata and controls
323 lines (176 loc) · 8.92 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File Operation:- Read,Write and Appnd
fp = open("input2.txt","w") # so in write mode even the file is not exist in particular directory then python will create the file by given name and we write in it.
fp.write("sample text line 1") #i am here specially for learning python ( in this command we write the statement which we want to metion in above named file )
"""content = fp.read()
print(content)"""#UnsupportedOperation: not readable
# So in the write mode i can only perform "WRITE" operation cannot be perfomed "READ" operaation
# & it's similar for rad mode also in "READ" mode i can only perform read operation and cannot perform "WRITE" operation
# So for avoidind such errors we have "w+" mode which stands for "WRITE as well as "READ" and "r+" mode which also stand for "READ" as well as "WRITE"
""" w+ = write + Read
r+ = read + wrire
"""
fp = open("input2.txt","w+")
fp.write("sample text line 1")
content = fp.read()
print(content) # this time it's not showig any error but also not printing content
# Here before writing anything in the file after creating the file, file pointer was at initial possition, then by using wrting command it reached at last possition and now it's ready for read the file that's why not showing any error
# That's why we use here two functions:- tell & seek
""" tell - uses for showing current fp possition
& seek - to change the fp possition
So, use of these both functions:- """
fp = open("input2.txt","w+")
print(fp.tell())# 0 ( initially, mouse pointer is present at initial location and ready to write,as the file created currently )
fp.write("Sample text line 1")
print(fp.tell())# 18 ( now,after writing the statement mouse pointer has reached on 18nth possition )
content = fp.read()
print(fp.tell())# 18 ( After, reading the satement mouse pointer at 18nth possition )
print(content)
""" seek function have two arguements:- (Offset,Position)
offset - indicates number of char
positin - 0 ==> Start of the file
1 ==> Current Possition
2 ==> End of the file
if seek(15,0) ==> having meaning change the fp(file pointer's possition) by 15 char from start of the file
seek(0,2) ==> change the fp(filepointer's possition) by0 char from end of the file
seek(15,1) & seek(15,2) are invalid
"""
fp = open("input2.txt","w+")
print(fp.tell())# 0 ( initial possitionof muouse pointer )
fp.write("Sample text line 1")
print(fp.tell())# 18 ( after write mouse pointer locality )
fp.seek(0,0)
print(fp.tell())# 0 ( after using seek funtion the location of mouse pointer has been chnaged
content = fp.read()
print(fp.tell())# 18
print(content)# Sample text line 1 ( and now it's able to read the content in write file )
# Similar approch in case of read (r+ = read + write)
fp = open("input.txt","r+")
content = fp.read()
print(content)
"""
Scholarship Notes
TSS Sameer Aatmanirbhar scholarship for Under Graduate 2021-2022
Protean e-Gov Renewal Scholarship Scheme for students pursuing B.E/B.Tech (2021-2022)
Publish Date : 15-03-20
Protean e-Gov Scholarship Scheme for students pursuing B.E/B.Tech (2021-2022)
Pragati Scholarship Programme For Girls Students pursuing Under Graduate Course
Arvind Fashions Limited ITI Scholarship Scheme 2021-2022
Publish Date : 24-02-2022
Arvind Fashions Limited UG Scholarship Scheme 2021-2022
Sandvik Coromant Girls Scholarship Program (21-22)
AIA Scholarship Programme For Undergraduate Course
AIA Scholarship Programme For B.E/B.Tech Course
Astral Foundation Scholarship For Undergraduate Courses (2021-2022)
Astral Foundation Scholarship For B.E/B.Tech Course (2021-2022)
Publish Date : 28-02-2022
Astral Foundation Renewal Scholarship For Under Graduate Course (2021-2022)
Astral Foundation Renewal Scholarship For B.E/B.Tech Course (2021-2022)
Timken Scholarship for B.E./B.Tech Students (2021-2022)
Timken Renewal Scholarship for B.E./B.Tech Students (2021-2022)
H.G. Infra Engineering Ltd. Scholarship for Undergraduate Courses
H.G. Infra Engineering Ltd. Scholarship for B.E./B.Tech Course
2 years programme:-
18000 only recorded
22000 live+recorded
1 year programme:-
Recorded 6-7 month-11000
+ live:- 16000
12 month:- 22000
"""# now it was about reading of the file by using r+ we can write something in this read file
fp = open("input.txt","r+")
content = fp.read()
print(content)
fp.write("\n\nSample line added using python script")
"""
Scholarship Notes
TSS Sameer Aatmanirbhar scholarship for Under Graduate 2021-2022
Protean e-Gov Renewal Scholarship Scheme for students pursuing B.E/B.Tech (2021-2022)
Publish Date : 15-03-20
Protean e-Gov Scholarship Scheme for students pursuing B.E/B.Tech (2021-2022)
Pragati Scholarship Programme For Girls Students pursuing Under Graduate Course
Arvind Fashions Limited ITI Scholarship Scheme 2021-2022
Publish Date : 24-02-2022
Arvind Fashions Limited UG Scholarship Scheme 2021-2022
Sandvik Coromant Girls Scholarship Program (21-22)
AIA Scholarship Programme For Undergraduate Course
AIA Scholarship Programme For B.E/B.Tech Course
Astral Foundation Scholarship For Undergraduate Courses (2021-2022)
Astral Foundation Scholarship For B.E/B.Tech Course (2021-2022)
Publish Date : 28-02-2022
Astral Foundation Renewal Scholarship For Under Graduate Course (2021-2022)
Astral Foundation Renewal Scholarship For B.E/B.Tech Course (2021-2022)
Timken Scholarship for B.E./B.Tech Students (2021-2022)
Timken Renewal Scholarship for B.E./B.Tech Students (2021-2022)
H.G. Infra Engineering Ltd. Scholarship for Undergraduate Courses
H.G. Infra Engineering Ltd. Scholarship for B.E./B.Tech Course
2 years programme:-
18000 only recorded
22000 live+recorded
1 year programme:-
Recorded 6-7 month-11000
+ live:- 16000
12 month:- 22000
Sample line added using python script """
# So i am able to write in the file after read
""" a ( append )
a+( append + )
"""
# difference betwen r,r+ & w,w+ & a,a+
""" so r,r+,w,w+ ==> whenever we open the file the file pointers(fp)'s location at start
a,a+ ==> the file pointer possition is defaultly at end
"""
fp = open("input.txt","r+")
print(fp.tell())#0 ( So, initialy the location of file pointer is at initial )
content = fp.read()
print(content)
fp = open("input.txt","a+")
print(fp.tell())# 1594 ( then, after using append+ command file posstion pointer at last of the file )
content = fp.read()
print(content)
# So, in r+ for 'READ' and in w+ for 'WRITE' first find the file pointers possition by TELL function then change it by using 'SEEK' function instead of this i can directly use "a+" method
""" difference b/w - a & - a+"""
fp = open("input.txt","a")
print(fp.tell())
content = fp.read() #UnsupportedOperation: not readable
""" but in the case of write """
fp = open("input.txt","a")
fp.write("\n\nShubham") # it automatically add the string which we want
# hence in append function we can only perform WRITE operationt NOT Read operation while in the case of a+ we can perform both "READ" as well as "WRITE" operations
"""
Scholarship Notes
TSS Sameer Aatmanirbhar scholarship for Under Graduate 2021-2022
Protean e-Gov Renewal Scholarship Scheme for students pursuing B.E/B.Tech (2021-2022)
Publish Date : 15-03-20
Protean e-Gov Scholarship Scheme for students pursuing B.E/B.Tech (2021-2022)
Pragati Scholarship Programme For Girls Students pursuing Under Graduate Course
Arvind Fashions Limited ITI Scholarship Scheme 2021-2022
Publish Date : 24-02-2022
Arvind Fashions Limited UG Scholarship Scheme 2021-2022
Sandvik Coromant Girls Scholarship Program (21-22)
AIA Scholarship Programme For Undergraduate Course
AIA Scholarship Programme For B.E/B.Tech Course
Astral Foundation Scholarship For Undergraduate Courses (2021-2022)
Astral Foundation Scholarship For B.E/B.Tech Course (2021-2022)
Publish Date : 28-02-2022
Astral Foundation Renewal Scholarship For Under Graduate Course (2021-2022)
Astral Foundation Renewal Scholarship For B.E/B.Tech Course (2021-2022)
Timken Scholarship for B.E./B.Tech Students (2021-2022)
Timken Renewal Scholarship for B.E./B.Tech Students (2021-2022)
H.G. Infra Engineering Ltd. Scholarship for Undergraduate Courses
H.G. Infra Engineering Ltd. Scholarship for B.E./B.Tech Course
2 years programme:-
18000 only recorded
22000 live+recorded
1 year programme:-
Recorded 6-7 month-11000
+ live:- 16000
12 month:- 22000
# Shubham
"""
# ********** Summary ****************** :-
# r => fp => start,file should already exist, read
# r+ => fp => Start,file should already exist,read + write
# w => fp => start,create a new file,write
# w+ => fp => tart,create a new file,write + read
# a => fp => end,create a new file, write at the end
# a+ => fp => end,create a new file, write + read