Python File Handlig
Here we discuss about how to handle text files
using python.
This function can be used to creating, reading, updating & deleting files.
Python File Open
open( ) - function is used to start file handling
and it takes two parameters.
Structure
myfile = open("file name","mode")
Here we learn about three main modes in file handling ,
1) Write(w)
2) Append(a)
3) Read(r)
Python Write - w
We can creat a new file using this function, in the
same location as the python file.
If the file already exist, data in it will be deleted and the new data will be
replaced.
.write() Function
.write() - function is used to write something to the text file.
Example - 1
myfile = open("names.txt","w")
myfile.write("John")
myfile.write("\n paul")
myfile.write("\n Anji \n Hafsa")
myfile.close()
\n - is used to creat a line break in python.
After execusion, the above code will creat the following file.
name.text
John
paul
Anji
Hafsa
Python append - a
We can creat a new file using this function, in the
same location as the python file.
If the file already exist, the new data will be added to the end of the file.
.write() Fuction
.write() - function is used to write something to the text file.
Example - 2
myfile = open("names.txt","a")
myfile.write("Chadwick")
myfile.write("\n Steve")
myfile.close()
\n - is used to creat a line break in python.
After execusion, the above code will update the name.text file as follows.
name.text
John
paul
Anji
Hafsa
Chadwick
Steve
Python read - r
We can read exisiting files using this function, in
the
same location as the python file.
If the file doesn't exist, an error is raised.
.read() Function
.read() - function is used to read something from the text file.
Example - 3
myfile =
open("names.txt","r")
#Assign the content in the file to a variable.
content = myfile.read()
print(content)
myfile
.close()
After execusion, the above code will fetch the name.text file's content and prompt the
following output.
Output
John
paul
Anji
Hafsa
Chadwick
Steve
.readline() function
When we need to fetch line by line from the
name.txt file use the below method.
.readline() - function is used to read a single lline from the text file.
Example - 4
myfile =
open("names.txt","r")
#Assign first line in the file to a variable.
content = myfile.readline()
print(content)
myfile
.close()
After execusion, the above code will fetch the name.text files' first line and
prompt the
following output.
We can use this method to fetch multiple lines as well as elements of lines
Example - 5
myfile = open("names.txt","r")
print(myfile.readline())
print(myfile.readline())
print(myfile.readline(2))
print(myfile.readline(3))
myfile.close()
After execusion, the above code will fetch the name.text files' lines and prompt
the
following output.
You can identify that how we can call the specific index of the line to segment it.
Output
John
paul
An
ji
Haf
sa
.readlines() function
.readlines() - function will assign the values the file to list.
Example - 6
myfile = open("names.txt","r")
result = myfile.readlines()
print(result)
myfile.close()
After execusion, the above code will fetch the name.text files' lines and prompt
the
following list.
Output
['John', 'paul', 'Anji', 'Hafsa', 'Chadwick', 'Steve']
Iteraring through lines of a file - for loop
.readlines() - function will assign the values the file to list.
Example - 7
myfile = open("names.txt","r")
for line in myfile:
print(line)
myfile.close()
After execusion, the above code will fetch the name.text files' lines and
prompt
the
following output.
Output
John
paul
Anji
Hafsa
Chadwick
Steve
.split() method
.split() - method will allow us to split(divide) a sting and assign each item into a
list.
Below code shows how to split a string from the "space".
line = "This is a random text"
list = line.split()
print(list)
Press Run to execute.
Below code shows how to split a string from the "=" mark. And extract items from the
list.
contacts.txt
john = 07783425627
Anji = 07862897602
paul = 07673920172
Example - 8
myfile = open("contacts.txt","r")
for x in myfile:
name = x.split("=")[0]
print(name)
myfile.close()
After execusion, the above code will fetch only the names of the contact holders from contacts.txt file and
prompt
the
following output.
Sequential Search
Search the names the in file to get the number as an output.
Example - 9
myfile = open("contacts.txt","r")
searchkey = input("search name -")
status - "No contacts found"
for x in myfile:
name = x.split("=")[0]
if (searchkey == name):
print(name)
status = "Above contacts are found"
print(status)
myfile.close()
Example output
Output
Search name - john
john = 07783425627
Above contacts are found.
#Next entry
Search name - hafsa
No contacts found