forked from arnavjindal/python-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile_Reading.py
130 lines (87 loc) · 2.44 KB
/
File_Reading.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
############ Open(), Read() & Readline() For Reading File #########
# file pointer(here f) returned by open function is used to read/write in file
# "rt" is default mode so it is optional to write
# Below is same as f = open("24. Tutorial.txt")
f = open("TUT_File_Reading.txt", "rt")
content = f.read()
print(content)
f.close()
# Good practice to close opened files
# Technically it is to frees/release all the resources
# using by file when it is open.
## Reading in binary mode
f = open("TUT_File_Reading.txt", "rb")
content = f.read()
print(content)
f.close()
## Reading fixed length of characters
f = open("TUT_File_Reading.txt", "rt")
content = f.read(4)
print(content)
content = f.read(4)
print(content)
f.close()
# Reading more than available characters
f = open("TUT_File_Reading.txt", "rt")
content = f.read(344555)
# It will print till available character
print("1 :",content)
content = f.read(344555)
# Nothing left to read now
print("2 :",content)
f.close()
## Printing character by character in each line
f = open("TUT_File_Reading.txt", "rt")
content = f.read()
for line in content:
print(line)
f.close()
# Output :
# W
# e
# a
# r
# e
# o
# n
# ...............
## Printing line by line using itreater in file
f = open("TUT_File_Reading.txt", "rt")
content = f.read()
for line in f:
print(line)
f.close()
# Output : Nothing because content has already read the file
f = open("TUT_File_Reading.txt", "rt")
# content = f.read()
for line in f:
print(line)
f.close()
# Output : (print by default give a backslash end character and file also have new line charcter when we write next line in file)
# We are on a mission to transform and Optimize World with Indian Technologies.
# We will work hard.
# We will win.
f = open("TUT_File_Reading.txt", "rt")
# content = f.read()
for line in f:
print(line,end="")
f.close()
# Output : (print bydefault give a backslash end character)
# We are on a mission to transform and Optimize World with Indian Technologies.
# We will work hard.
# We will win.
##### readline function to read one line a time
f = open("TUT_File_Reading.txt", "rt")
print(f.readline())
f.close()
# Output :
# We are on a mission to transform and Optimize World with Indian Technologies.
f = open("TUT_File_Reading.txt", "rt")
print(f.readline())
print(f.readline())
print(f.readline())
f.close()
########## Readlines Function to get list of lines.
f = open("TUT_File_Reading.txt", "rt")
print(f.readlines())
f.close()