How to create a file in python?
Files provide us a convenient way of storing our information. Once we have our data, the next task is to store it so that it can be accessed in the future for various purposes. And here comes ‘files’ to rescue us from the incessant worry of unorganized work.
In python, we can read them, write them and append them as we do normally with our files.
Here we will discuss how we can-
- Create a file in Python
- Open a file in Python
- Append a file in Python
- Read a file line by line
- File modes present in Python
Before this let us understand some basic terminologies which act as a prerequisite to grasp the concept of File Handling in Python.
Absolute and Relative Path
An absolute path is the one that tells us about the exact location of our file with the help of a complete directory list whereas when we talk about a relative path we have to merge it with another path for accessing our file.
The above-given path is the absolute path.
Subjects\Tutorial&Examples is the example of the relative path as it gives us the complete information when it merges with other details of the path.
Types of Files
There are two types of files-
- Text Files
- Text files are a collection of characters that are processed sequentially.
- Reading, writing, and appending are the operations that can be performed on such files.
- Binary Files
- Binary files are those files that contain all types of data in the binary format for further processing.
- Examples of binary files are images, spreadsheets, videos, etc.
- They are a collection of bytes.
Creating a File in Python
A file can be created in Python using the append mode. If we open a file and specify the access mode as ‘a’ then it would create a file if it doesn’t exist.
fobj = open(“Mydoc.txt”,”a”)
Now, let us see how we can open a file in Python-
Files can be opened in Python using the open() function. This function creates an object which is used to call methods.
The syntax of open() function is-
fobj=open(“filename”, “access mode”)
The filename is the name of the file which we want to access.
Access mode refers to the mode in which we would like to open it.
We will discuss various access modes in the later section of this article.
Next is to see how we can close a file in Python-
The syntax for closing a file is-
Features of close() method-
- It closes the file object.
- Once we use this, we can't perform a read or write operation on our file.
- It frees up the allocated resources of the system.
Appending a file in Python
Appending means adding more content to a file that is made already. We can use the access modes 'a' or 'ab' to append a file depending upon its type (text or binary).
The syntax for the same is-
fobj = open(“fp.txt”,”a”)
If we specify the access mode as “w” or “wb” then it will overwrite the contents of our existing file.
Let us see how we can read a file in Python
The files in Python can be read using two methods-
- read() – It is used to read a string in an opened file.
- readline() – It is used to read a single line from the file.
Implementation of read() method
fobj = open(“fp.txt”, “r”) print(fobj.read(9)) fobj.close() Implementation of readline() method fobj = open(“fp.txt” , “r”) print(“Line 1: “,fobj.readline()) fobj.close()
Types of File Mode present in Python:
- r – this opens the file to read only and it is the default mode.
- rb – this opens a file to read in binary format.
- r+ - this opens the file for reading & writing.
- rb+ - this opens the file for reading & writing in binary format.
- w – it opens the file to write only. If the file doesn't exist it is created using this or if it exists the contents are overwritten.
- w+ - it opens the file for reading and writing. If the file doesn't exist it is created using this or if it exists the contents are overwritten.
- wb - this opens the file for reading & writing in binary format.
- a – it opens a file to append.
- ab – the file is opened in binary format for appending.