@@ -20,16 +20,18 @@ Example:
2020fil= open (" mylog.txt" ," r" )
2121content= fil.read()
2222print (content)
23-
24- # Result:Hi, hello i am from python file.
23+ ```
24+ Output:
25+ ```
26+ Hi, hello i am from python file.
2527```
2628
2729### How to read some content from a file
28- - for ** read()** method you can pass a integer length of character (number that shows how many characters do you want to read)
29- - after read statement python cursor will positioned according to given input to ** read()** function.
30+ - for ** read()** method you can pass an integer length of character (a number that shows how many characters do you want to read)
31+ - after reading statement python cursor will position according to given input to ** read()** function.
3032
3133- we can manipulate the python file cursor position using ** seek()** function
32- - ** tell()** function is used to getting the current position of cursor in file.
34+ - ** tell()** function is used to getting the current position of the cursor in file.
3335
3436Syntax:
3537``` python
@@ -52,6 +54,10 @@ content=fil.read(2)
5254print (content)
5355# Result:Hi
5456```
57+ Output:
58+ ```
59+ Hi
60+ ```
5561
5662
5763** mylog.txt** contains
@@ -65,38 +71,71 @@ fil=open("mylog.txt","r")
6571
6672# get cursor position
6773cur= fil.tell()
74+ print (" Current Position of Cursor" )
6875print (cur)
6976# Result:0
7077
7178# Read 2 character from file
7279fil= open (" mylog.txt" ," r" )
7380content= fil.read(2 )
81+ print (" \n Read 2 character from file" )
7482print (content)
7583# Result:Hi
7684
7785# Now get updated cursor position
7886cur= fil.tell()
87+ print (" \n updated cursor position" )
7988print (cur)
8089# Result:2
8190
8291# Read next 7 character from file
8392content= fil.read(7 )
93+ print (" \n Read next 7 character from file" )
8494print (content)
8595# Result: , hello
8696
8797# now get the updated cursor position
8898cur= fil.tell()
99+ print (" \n updated cursor position" )
89100print (cur)
90101# Result:9
91102
92103# now move cursor to 0 position which is start of file
104+ print (" \n now move cursor to 0 position which is start of file" )
93105fil.seek(0 )
106+
107+ # Print Current Cursor Position
108+ print (" \n Current Cursor Position" )
109+ print (fil.tell())
110+ ```
111+
112+ Output:
94113```
114+ Current Position of Cursor
115+ 0
95116
117+ Read 2 character from file
118+ Hi
96119
120+ updated cursor position
121+ 2
97122
98- ### How to read line from a given file
99- - ** readline()** function is used to read line from a file.
123+ Read next 7 character from file
124+ , hello
125+
126+ updated cursor position
127+ 9
128+
129+ now move cursor to 0 position which is start of file
130+
131+ Current Cursor Position
132+ 0
133+ ```
134+
135+
136+
137+ ### How to read a line from a given file
138+ - ** readline()** function is used to read a line from a file.
100139- By calling ** readline()** three times, you can read the three first lines.
101140
102141Syntax:
@@ -113,16 +152,22 @@ Fourth line data 4
113152```
114153Example:
115154``` python
116- # read first line
155+ # Create File Object For Reading Purpose
117156fil= open (" mydata.txt" ," r" )
157+
158+ # Read First File From File
118159firstline= fil.readline()
119- print (firstline)
120160
121- # Result :First line data 1
161+ # Print First File
162+ print (firstline)
163+ ```
164+ Output:
165+ ```
166+ First line data 1
122167```
123168
124169### How to read all lines from a file
125- - ** readlines()** is return list object with every line as element
170+ - ** readlines()** is return list object with every line as an element
126171
127172Syntax:
128173``` python
@@ -143,14 +188,19 @@ Example:
143188# initialize file for reading purpose
144189fil= open (" mydata.txt" ," r" )
145190
146- # read all lines
191+ # read all lines and Store it in list
147192lines= fil.readlines()
193+
194+ # Print All Lines List
148195print (lines)
196+ ```
149197
150- # Result: ['First\n','Second\n','Third']
198+ Output:
199+ ```
200+ ['First\n', 'Second\n', 'Third']
151201```
152202
153- ### Read all file using with statement
203+ ### Read all file using " with" statement
154204
155205File ** mylog.txt** contains
156206```
@@ -159,15 +209,18 @@ Hi, hello i am from python file.
159209
160210Example
161211``` python
212+ # Read File using with loop
162213with open (" mylog.txt" ," r" ) as fil:
163- content= fil.read()
164- print (content)
165-
166- # Result:Hi, hello i am from python file.
214+ content= fil.read()
215+ print (content)
216+ ```
217+ Output:
218+ ```
219+ Hi, hello i am from python file.
167220```
168221
169222
170- ### How to use for loop for read all multi line
223+ ### How to use for loop for reading all multi- line
171224file ** mydata.txt** contains
172225```
173226First line data 1
@@ -180,17 +233,18 @@ Example:
180233``` python
181234# Initilize file object
182235fil= open (" mydata.txt" ," r" )
183- for line in fil:
184- print (line)
185236
186- '''
187- # Result:
237+ # Use for loop Print line by line
238+ for line in fil:
239+ print (line)
240+ ```
241+ Output:
242+ ```
188243First line data 1
189244
190245Second line data 2
191246
192247Third line data 3
193248
194249Fourth line data 4
195- '''
196250```
0 commit comments