Types of Files in Python
- Text File
- Binary File
1. Text File
- Text file store the data in the form of characters.
- Text file are used to store characters or strings.
- Usually we can use text files to store character data
- eg: abc.txt
2. Binary File
- Binary file store entire data in the form of bytes.
- Binary file can be used to store text, image, audio and video.
- Usually we can use binary files to store binary data like images,video files, audio files etc
Opening a File
- Before performing any operation (like read or write) on the file,first we have to open that file.For this we should use Python's inbuilt function open()
- But at the time of open, we have to specify mode,which represents the purpose of opening file.
- We should use open() function to open a file. This function accepts 'filename' and 'open mode' in which to open the file.
- file handler = open("file name", "open mode", "buffering")
- f = open(filename, mode)
The File Opening Mode
w
- To write data into file. If any data is already present in the file, it would be deleted and the present data will be stored.
- open an existing file for write operation. If the file already contains some data then it will be overridden. If the specified file is not already available then this mode will create that file.
r
- To read the data form the file. The file pointer is positioned at the beginning of the file.
- open an existing file for read operation. The file pointer is positioned at the beginning of the file.If the specified file does not exist then we will get FileNotFoundError.This is default mode.
a
- To append data to the file. Appending means adding at the end of existing data. The file pointer is placed at the end of the file. If the file does not exist, it will create new for writing data.
- open an existing file for append operation. It won't override existing data.If the specified file is not already avaialble then this mode will create a new file.
w+
- To write and read data a file. The previous data in the file will be deleted.
- To write and read data. It will override existing data.
r+
- To read and write data into the file. The previous data in the file will not be deleted.The file pointer is placed at the beginning of the file.
a+
- To append and read data from the file.It wont override existing data.
- To append and read of a file. The file pointer will be at the end of the file if the file exists. If the file does not exist, it creates a new file for reading and writing.
x
- To open a file in exclusive creation mode for write operation. If the file already exists then we will get FileExistsError.
Note : All the above modes are applicable for text files. If the above modes suffixed with 'b' then these represents for binary files.
- Eg: rb,wb,ab,r+b,w+b,a+b,xb
- f = open("abc.txt","w")
We are opening abc.txt file for writing data.
It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read. Best types of nat service provider.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete