- What is File Handling In Python?
- Common functions used in file handling in python:
- Common access modes of python file handling:
- Example 1: Creating a text file
- Example 2: Writing a file
- Example 3: Reading a file
- Example 4 : Appending a file
- Other access modes for python file handling:
- Methods used with file positions:
- More file operations
- Directory operations
What is File Handling In Python?
File Handling In Python– Python is generally preferred for its simplicity. Similarly, file handling concepts that are tougher in many other object-oriented languages is also made easy in python. Python files can be written both in the text as well as binary.
Download notes on file handling in python
The general syntax of working with files:
file_object = open(filename,access_mode)
file_object – indicates the object created for the file
open() – this method opens the file and performs the operation based on the access_mode
filename – the file which needs to be manipulated
access_mode – specifies the mode of operation such as read, write or append
Common functions used in file handling in python:
There are 4 common functions used in python file handling without which any operations in files cannot be performed. They are:
- read() – reads the file that has been passed to the file object
- write() – writes the content specified to the file
- open() – opens the file
- close() – closes the file that has been opened. It is always a good practice to close the file at the end.
Common access modes of python file handling:
There are 4 common modes of operation for file handing in python:
- x– indicates the creation of file
- r– indicates that the file has to be read
- w– indicates to write contents in the file. If the file already exists, then it just overwrites the file with the current contents.
- a– indicates that contents need to be appended. The file pointer will be at the end of the file. If the specified file is not present then it creates a new file and the file pointer will be at the start of the file.
Example 1: Creating a text file
A sample.txt file will be created in the folder the python file is located
Example 2: Writing a file
f=open("sample.txt","w") f.write("File handling in python") f.close()
The contents are written in the sample.txt file
Output in notepad–
Example 3: Reading a file
f=open("sample.txt","r") print(f.read()) f.close()
File handling in python
Example 4 : Appending a file
f=open("sample.txt","a") f.write("… Happy learning….") f.close()
Updated data in sample.txt will be==> File handling in python… Happy learning….
Other access modes for python file handling:
- rb – The file is read in binary format.
- r+ – the file can be used for both reading and writing contents in text format
- rb+ – the file can be used for both reading and writing contents in binary format
- wb – the file can be written in binary format
- w+ – the file can be used for both reading and writing in text format
- wb+ – the file can be used for both reading and writing in binary format
- ab – the file contents can be appended in binary format
- a+ – the file can be used for both reading and appending contents in text format
- ab+ – the file can be used for both reading and appending contents in binary format
Methods used with file positions:
There are 2 file methods that are used to deal with file positions:
The tell() is used for identifying which position the current file pointer is located. In other words, it’s the distance of the file pointer from the beginning of the file.
f=open("sample.txt","r") print(f.read()) # reads the file print(f.tell()) # the file is read thus the file pointer # is now at the end of the contents # in the file f.close()
File handling in python… Happy learning…. 45
The seek() method is used to change the current file position. This function can be written with either 1 or 2 parameters.
file_object(offset) file_object (offset, from)
offset – the offset value indicates the number of bytes or location of the file pointer to be moved. A positive number indicates right to left and a negative number indicates left to right.
from – the from parameter can be of only 3 values. If it is 0, then it indicates the pointer must be at the beginning of the file. If its 1, then the current position of the file pointer has to be considered. If it is 2, then it indicates the end of the file.
f=open("sample.txt", "r") print(f.read()) f.seek(20) print(f.tell())
File handling in python... Happy learning.... 20
f=open("sample.txt", "r") f.seek(5,0) print(f.read())
handling in python... Happy learning....
More file operations
The os module can be used for file operations and directory related operations in python. To perform those operations, the os module needs to be imported first.
- rename()– can be used to rename a file
- remove() – remove or delete a file
os.rename(old_file_name. new_file_name) os.remove(file_name)
import os os.rename( “sample.txt”, ”example.txt)
The text file sample.txt is changed to example.txt
import os os.remove(“example.txt”)
The example.txt file is deleted
There are 4 common directory operations which you can perform in python file handling:
- mkdir() – creates a new directory
- chdir() – changes the directory
- getcwd() – returns the current directory
- rmdir() – removes the directory
import os os.mkdir(“practice”)
Creates a new directory called practice
import os os.chdir(“/desktop/sample”)
Changes the location of the directory to the specified path
import os os.getcwd()
Returns the current working directory
import os os.rmdir(“/sample”)
Removes the specified directory
This post is contributed by Kushmitha Radhakrishnan.
So today we will learn how to auto login in a tab using Python. So, let’s first see…
In python, it’s very easy to deal with matrices due to its simple syntax and we create matrices…
Hello friends, do you know that there are many ways to make music players from simple to advanced…
Hello guys welcome again to our site where you get amazing source code absolutely free with explanation So,…
Hello friends, today we will make an age calculator using python’s GUI library tkinter so, let’s start. Python…
Hello guys, as usual we are sharing one more source code using which you can display images inside…