-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0062_file_IO_intro.py
More file actions
47 lines (35 loc) · 1.03 KB
/
0062_file_IO_intro.py
File metadata and controls
47 lines (35 loc) · 1.03 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
print()
# opening, reading, and explicitly closing the file
jabber = open("./data/sample.txt",mode="r")
for line in jabber:
if "jabberwock" in line.lower():
print(line, end='')
jabber.close()
print()
# no need to explicitly close a file.
# 'with' does that
with open("./data/sample.txt", mode="r") as jabber:
for line in jabber:
if "JAB" in line.upper():
print(line, end="")
print()
# print each line
with open("./data/sample.txt", "r") as jabber:
line = jabber.readline()
while line:
print(line, end="")
line = jabber.readline()
print()
# print each line
with open("./data/sample.txt", "r") as jabber:
lines = jabber.readlines() # access all the file text in one go
print(lines) # prints the lines as elements in the list
for line in lines:
print(line, end="")
print()
# print each line
with open("./data/sample.txt", "r") as jabber:
lines = jabber.readlines()
print(lines) # prints the lines as elements in the list
for line in lines[::-1]:
print(line, end="")