File handling in python
File handling in python is an important part of any application development. Python has a number of functions that can be used for creating, reading, updating, and deleting files. The concept of file handling has been implemented over various other computer languages, but their implementation is either complicated or lengthy. But the concepts of file handling in Python, is easy and short.
File handling in python is an important part of any application development. Python has a number of functions that can be used for creating, reading, updating, and deleting files. The concept of file handling has been implemented over various other computer languages, but their implementation is either complicated or lengthy. But the concepts of file handling in Python, is easy and short. Python treats file differently as text or binary files which are important. We will learn two types of files that can be handled in Python:
1) Text Files
2) Binary Files
Binary Files: It contains binary language,0s and 1s.
Text files: In this type of file, each line of text is terminated with a special character called EOL (End of Line), which is the new line character ('\n') in python by default.
Opening file in python
1. We use open () function in Python to open a file in read or write mode.
2. open ( ) will return a file object.
3. To return a file object we use open() function along with two arguments, that accepts file name and the mode, whether to read or write.
The Syntax: file_object = open(filename, mode)
4. The kinds of mode, that Python provides to access different files are,
“ r “, for reading.
“ w “, for writing.
“ a “, for appending.
5. The mode argument is not mandatory. If not passed, then Python will assume it to be “ r ” by default.
Different File modes in python:
|r||Opens a file in read mode for reading files.|
|rb||Opens a file in read binary mode for reading files in binary format.|
|r+||Opens a file in read plus mode for both reading and writing into a file.|
|rb+||Opens a file in read binary plus mode for both reading and writing in files in binary format.|
|w||Opens a file in write mode for writing in a file.|
|wb||Opens a file in write binary mode for writing files in binary format..|
|w+||Opens a file in write plus mode for both writing and reading from a file.|
|wb+||Opens a file in write binary plus mode for both writing and reading in binary format.|
|a||Opens a file in append mode for appending into a file.|
|ab||Opens a file in append binary mode for appending files in binary format.|
|a+||Opens a file in append plus mode for both appending and reading in a file.|
|ab+||Opens a file in append binary plus mode for both appending and reading in binary format.|
Some methods of file object in Python :
|File_obj.close()||Used to close an open file object.|
|File_obj.flush()||Used to flush the internal buffer of the system.|
|next(File_obj)||Each time “next()” is called it returns the next line from the file.|
|File_obj.read([size])||Used to read a specified number of bytes from the file.|
|File_obj.readline()||It is used to read one entire line from the file.|
|File_obj.readlines()||Used to read line until EOF (End Of File) and returns a list containing the lines.|
|File_obj.seek(offset, from)||Used to Set the cursor at file's current position.|
|File_obj.tell()||Used to return the file's current position|
|File_obj.write(str)||It is used to writes a string to the file. There is no return value.|
Closing an open file in python:
1. The close() method in python closes an opened file. A file which is closed cannot be read or written.
2. Any operation, which requires that the file be opened will raise a ValueError after the file has been closed.
3. It is always a good practice to use the close() method to close a file at the end.
The tell() method in python tells the current position within the file; i.e. the next read or write operation in the file will occur at that many bytes from the beginning of the file.
The seek(offset, from) method in python changes the current file position. The offset argument/parameter indicates the number of bytes to be moved. The from argument/parameter specifies the reference position from where the bytes are to be moved as specified in offset argument. If from is set to 0, it means use the beginning of the file as the reference position, if from is set to 1, it means use the current position as the reference position and if the from is set to 2 then use the end of the file as the reference position.
Handling Binary files :
Opening a Binary File:
To open a file in binary format, add 'b' to the mode parameter in the open() function. Hence, the "rb" mode opens the file in binary format for reading, and the "wb" mode opens the file in binary format for writing. Different from text mode files, binary files are not in human readable format.
num=[5, 10, 15, 20, 25]
The built-in function bytearray() in python returns a byte representation of the object.
Reading a Binary File:
To read a binary file in human understandable format, the output of the read() method is casted to a list using the list() function.
print (num) f.close()
Difference between text file and binary file:
|Text File||Binary File|
|Text file is Bit representation of characters.||Binary file is Bit representation of custom data.|
|Less susceptible to data corruption as change reflects as soon as made and can be undone.||Can easily get corrupted, corrupt on even single bit change|
|Text file is used to Store only plain text in a file.||Binary file can store different types of data (audio, text,image) in a single file.|
|Widely used file format and can be opened in any type text editor.||Developed for an application and can be opened in that application only.|
|Mostly .txt is used as extensions for text files.||Can have any application defined extension, e.g. .bin, .dat etc.|
Python File Handling
Binary file in python
different files in python
modes in python file
Total 278 views