In this article, we will learn about Create, Open, Close files in Python and different and file opening modes. Python provides inbuilt functions for creating, writing, and reading files to perform file handling operations.
So in this article, we are going to first learn ‘How to create a text file’, then ‘How we can open a file, ‘How we can close a file, ‘What are the file opening modes’.
How to Create a Text File?
In Python, we use the open() function to create a file. This function takes two arguments
- File-path: The first argument is the path with the file name. If we provide only the file name then it creates the file in the current working directory.
- mode: The second argument is an important argument as it decides the mode in which we are trying to open/create the file.
Different File Modes in Python
So let us understand the different modes that Python files support.
|“r” – Read||This is the default value. When we pass “r” as mode then it means to Open a file for reading purposes,If the file does not exist then we get the error.|
|“a” – Append||“a” means the append mode, which means to open a file for appending. appending means we want to add more content to the existing file. If the file does not exist then the Open function call creates the new file.|
|“w” – Write||“w” means writing more, which means to open a file for writing. If the file does not exist then Open|
function call creates the new file.
|“x” – Create||“x” means to Create the specified file, but if the file already exists then this mode returns an error.|
|“+”||This will open a file for reading and writing both at the same time. This can be used with both ‘r’ and ‘w’ as ‘r+’ and ‘w+’.|
File Handling Format in Python
There are some more modes in Python that are used for file handling. These additional modes are used to specify the file handling format.
Python files can be handled in binary or text mode. We can specify this model in the open function and decide which type of data handling we want for our file.
- “t” – Text – “t’ means text mode, and this is the default value if we don’t specify any other mode.
- “b” – Binary – “b” means the binary mode.
Example: How to Create a new Text File?
- In this example, we are trying to create a ‘samplefile.txt’. We are only passing the file name which means this will be created in the current working directory. But if we would like to specify the full path, then we can do that also.
- The second argument which we are passing the mode, which is ‘x’ means we want to create this file.
file = open("samplefile.txt","x")
Note : If the file already exists then you will get an error while calling this function. The interpreter will complain:Error::thrown if file already Exist
file = open("samplefile.txt","x") FileExistsError: [Errno 17] File exists: 'samplefile.txt'
How to Open a Text File?
In this example, we will learn how to open an existing file and get file content access. We will use the Open() function for this purpose also. In the open() function we will pass the file name with its path and the mode in which we would like to open the file.
We can open a file for the read-only purpose or we can specify mode as read-write where we have both reading and writing access to the file.
Example : Open File in Read-only Mode
#open python file in Read-only Mode file_open = open("samplefile.txt","r")
Example : Open File in read-write Mode
#Open File in read-write Mode file_open = open("samplefile.txt","w")
How to close a Python File?
The close() method is used to close a file if open using the file object. Once we have done writing the data into a file It is good practice to close the file to free the memory space.
We do not need to provide the file name for the close function because the object on which we are calling this close() function knows the file name and it has the file associated with it.
So simply we can call object.Close() function and then the file will get closed for any further operations.
The close() method is not a safe method to close a file . The best way to close a file using with statement.
with open('file_path') as file_open: