-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp18.py
More file actions
401 lines (219 loc) · 11.2 KB
/
p18.py
File metadata and controls
401 lines (219 loc) · 11.2 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# Read text file
# open function
# read method
# seek method
# tell method
# readline method
# readlines method
# close method
# path=(r'F:\Courses\python hindi\harshit vashisth\file1.txt')
f=open('file1.txt') # here i have to put txt file's path if .py file and .txt file are not in same folder.
# f=open('file1.txt','r') ---for read file, f=open('file1.txt','w') ---for write file, f=open('file1.txt')----bydefault is reading mode
print(f.read()) # f.read() will return me the file data in string form
f.close()
# o/p:
# -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
# -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
f1=open('file1.txt') # o/p: -------------- Important things to remember -----------------
print(f1.read()) # -- never do something to impress someone.
print(f1.read()) # -- never live your life on someone else opinions.
f1.close() # -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
# python will return file data only one time even though we wrote print(f1.read()) two times.
# this is happen because python read text file according to cursor, hemce when second time print(f1.read()) executed, our cursor was on last position in file, therefore we got data only one time
f2=open('file1.txt') # o/p: cursor position - 0
print(f"cursor position - {f2.tell()}") # -------------- Important things to remember -----------------
print(f2.read()) # -- never do something to impress someone.
print(f"cursor position - {f2.tell()}") # -- never live your life on someone else opinions.
print(f2.read()) # -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
f2.close() # cursor position - 250
# tell() method is used for getting position of cursor
# seek() method----> it is used for changing the position of cursor
f3=open('file1.txt')
print(f"cursor position - {f3.tell()}")
print(f3.read())
print(f"cursor position - {f3.tell()}")
print("Before seek method")
f3.seek(0)
print("After seek method")
print(f"cursor position - {f3.tell()}")
print(f3.read())
f3.close()
# o/p: cursor position - 0
# -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
# -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
# cursor position - 248
# Before seek method
# After seek method
# cursor position - 0
# -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
# -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
# readline() method ----> read one line at a time
f4=open('file1.txt')
print(f4.readline())
f4.close() # o/p: -------------- Important things to remember -----------------
f4=open('file1.txt')
print(f4.readline())
print(f4.readline())
print(f4.readline())
f4.close()
# o/p: -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
f4=open('file1.txt')
print(f4.readline(),end='') # to remove space between two line
print(f4.readline())
print(f4.readline())
f4.close()
# o/p: -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
# readlines() method ----> it will make list of lines which are in file
f4=open('file1.txt')
lines=f4.readlines()
print(lines)
print(len(lines)) # o/p: 4
f4.close() # o/p: ['-------------- Important things to remember -----------------\n', '-- never do something to impress someone.\n', '-- never live your life on someone else opinions.\n', "-- Just find whatever you love and then hustle untill you don't need to introduce yourself."]
f4=open('file1.txt') # o/p: -------------- Important things to remember -----------------
lines=f4.readlines() # -- never do something to impress someone.
for line in lines: # -- never live your life on someone else opinions.
print(line,end='') # -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
f4.close()
# name, closed ----> data descripter
f4=open('file1.txt')
print(f4.name) # o/p: file1.txt
print(f4.closed) # o/p: False
f4.close()
print(f4.closed) # o/p: True
# without using read function, f4 is iterable
f4=open('file1.txt') # o/p: -------------- Important things to remember -----------------
for line in f4: # -- never do something to impress someone.
print(line,end='') # -- never live your life on someone else opinions.
f4.close() # -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
# i can make slicing with the help of readlines() method
f4=open('file1.txt') # o/p: -------------- Important things to remember -----------------
for line in f4.readlines()[:2]: # -- never do something to impress someone.
print(line,end='')
f4.close()
# With block ----> it is used for reading and writing file and many other purposes also
# it is context manager
# in this, we don't need to use close() method to close our file and in add to it, context manager will handle our file in case of damage file
with open("file1.txt") as f5:
data=f5.read()
print(data)
print(f5.closed) # o/p: True
# o/p:
# -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
# -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
# -------------- Important things to remember -----------------
# -- never do something to impress someone.
# -- never live your life on someone else opinions.
# -- Just find whatever you love and then hustle untill you don't need to introduce yourself.
# 'r' mode ---> used to read file. it is bydefault mode
# 'a' mode ---> append mode, not delet file data, in fact append new data and old data
# 'w' mode ---> override the data(delete the old data and write new data), use when file is empty or we want to delete file data
# 'r+' mode---> we can read and write using this mode, it will not create file, it will start override from starting of the old data but not delete all old data, so we should use seek to set cursor position
# Write to file
# with open("file1.txt",'w') as f6: # w,a mode also create the file if file is not at location
# data=f6.write("amckmdla") # for a mode--->it will write 1 instead of data in file
# with open("file1.txt",'r+') as f6:
# f6.seek(len(f6.read()))
# f6.write("yash \n ")
# Read and Write ---> read one file and write those data in another file
# here file1.txt have some data and file2.txt is empty
with open("file1.txt","r") as rf:
with open("file2.txt","w") as wf:
wf.write(rf.read())
# when i executed code, file2.txt get data of file1.txt
# Excercise 1
with open("file1.txt","r") as rf:
with open("file2.txt","w") as wf:
for i in rf:
wf.write(f"{(i).split(',')[0]}\'s salary is {(i).split(',')[1]}")
# OR
with open("file1.txt","r") as rf:
with open("file2.txt","w") as wf:
for line in rf.readlines():
name, salary=line.split(',')
wf.write(f'{name}\'s salary is {salary}')
# Excercise 2
with open("index.html","r") as rf:
with open("link.txt","w") as wf:
for line in rf.readlines():
if "<a href=" in line:
pos=line.find("<a href=")
first_quot=line.find("\"",pos)
second_quot=line.find("\"",first_quot+1)
website=line[first_quot+1:second_quot]
wf.write(website+"\n")
# Better way
with open("index.html","r") as rf:
with open("link.txt","w") as wf:
page=rf.read()
# link_exist=True
while True:
pos=page.find("<a href=")
if pos== -1:
break
# link_exist=False
else:
first_quot=page.find("\"",pos)
second_quot=page.find("\"",first_quot+1)
web=page[first_quot+1:second_quot]
wf.write(web+"\n")
page=page[second_quot:]
# Read Love Story
with open('file1.txt','r') as rf:
print(rf.encoding)
data=rf.read()
print(data)
# # file1.txt
# Love Story
# chat start
# 😘😗😇💓😗
# chat end
# Note - Real friends don't need to send this emojis !
# One more Note - after your family, dogs love human being most but they can't send you emojis !
# Second last note - if someone sends you thse emojis just say :) and don't get over exited !
# Fissl Note - I love you, subscribe to this channel now !
# o/p: cp1252
# Love Story
# chat start
# 😘😗😇💓😗
# chat end
# Note - Real friends don't need to send this emojis !
# One more Note - after your family, dogs love human being most but they can't send you emojis !
# Second last note - if someone sends you thse emojis just say :) and don't get over exited !
# Fissl Note - I love you, subscribe to this channel now !
# to read emojis we should use encoding
with open('file1.txt','r',encoding='utf-8') as rf:
print(rf.encoding)
data=rf.read()
print(data)
# o/p: utf-8
# Love Story
# chat start
# �����😇💓💓😗😗
# chat end
# Note - Real friends don't need to send this emojis !
# One more Note - after your family, dogs love human being most but they can't send you emojis !
# Second last note - if someone sends you thse emojis just say :) and don't get over exited !
# Fissl Note - I love you, subscribe to this channel now !
# if our file has number of char and if we read then it will not memory efficient
with open('file1.txt','r') as rf:
data=rf.read(100)
print(data)
# o/p: it will return only 100 char from file
# to read whole file
with open('file1.txt','r') as rf:
data=rf.read(10)
while(len(data)>0):
print(data)
data=rf.read(10)