File handling
operations in Python.
RAM (Random Access Memory) is a volatile memory and will
wipe out all the information once the system is shut down.
In such a scenario, the file is used to store the
information permanently on the disk (hard disk) and will be available in case
of system shutdown.
The file handling in python consists of three processes:-
1.) Opening the file.
2.) Reading/Writing the file.
3.) Closing the file.
The Closing process is essential as it will freed the
resources allocated to it.
So, let us begin our journey on the topic called File.
Opening a file in
Python.
openfile = open("irisdata.txt")
Modes
|
Description
|
r
|
Open text file for reading. The stream is positioned at the
beginning of the file.(default)
|
w
|
Truncate file to zero length or create text file for writing
(in case the file is not present). The stream is positioned
at the beginning of the file.
|
a
|
Open the file for appending. Creates the file in
case
|
+
|
Open a file for both reading and
writing
|
Courtesy :- Stackoverflow.com
Closing the file in python:-
After reading the file, the best practice is to close the file.
Closing the file will release the resources attached with it.
openfile = open("irisdata.txt")
openfile.close()
However ,this method is not safe
as there is a chance that there may be some exception.
So , we will have some other way
to handle it.
try:
openfile = open("irisdata.txt")
finally:
openfile.close()
Python has its own way to handle such file closing operation
without using the close() function with the help of the with statement. Let us
check this with an example :-
with open('test.txt','w') as f :
f.write('hi,this
is me')
Reading the file in
Python :
There are various methods which is available for reading the file in python.
We can read the file line by line using the for loop.
Example:-
openfile = open('testdata.txt')
for i in openfile:
print(i)
#Output :-
This is a test.
We are learning.
python is a good programming language.
The second way is to learn via read() method .with the
read() method , we can define the size means if we read(30) ,it will read first
30 data.
Example :-
openfile = open('testdata.txt')
openfile.read(6)
#Output :-
This I
It means that the read will read first 6 data (This + Space
+ i).
However, if we do not mention the size ,then it will read
the whole data.
Example :-
openfile = open('testdata.txt')
openfile.read()
#Output :-
This is a test.
We are learning.
python is a good programming language.
Now , we can have two more interesting functions which are
associated with the file that are tell()
and seek().
Tell() ---provides you the current cursor location while
seek() will change the current cursor location.
Example :-
openfile = open('testdata.txt')
print(openfile.read(15))
print(openfile.tell())
openfile.seek(0)
print(openfile.read(4))
#Output :-
This is a test.
15
This
In the above example, the
first 15 charcters are read . and to find the cursor position we can use
the tell() method which tell us that the current cursor loication is at 15th
position seek(0) will bring the cursor
back to position 0.
readline() is to read
individual lines in a file. This method reads a file till the newline,
including the newline character.
Example :-
openfile = open('testdata.txt')
print(openfile.readline())
#Output :
This is a test.
readlines() method returns a list of remaining lines of the
entire file. All these reading method return empty values when end of file
(EOF) is reached.
Example :-
openfile = open('testdata.txt')
print(openfile.readlines())
#output:-
['This is a test.\n', 'We are learning.\n', 'python is a
good programming language.']
No comments:
Post a Comment