Introduction of File and Type of File
M3-R5.1 · Chapter 7: File Processing · 5 min read
Files: File is a named location on the disk (system) storage which records data for later access. It is used to store data permanently for future use to perform various file operations like opening, reading, writing etc. We can access the stored information (non-volatile) after the program termination.
File processing in Python refers to the ability to create, open, read, write, update, and manipulate files on your system using built-in functions and methods. It’s how Python interacts with files like .txt, .csv, .json, and many others.
In Python, files are treated in two modes as text or binary. The file may be in the text or binary format, each line of a file is terminated with a special character, called the EOL or End of Line characters like comma {,} or newline character. It ends the current line and tells the interpreter a new one has begun.
Advantages of File Handling in Python
Versatility: File handling in Python allows you to perform a wide range of operations, such as creating, reading, writing, appending, renaming, and deleting files.
Flexibility: File handling in Python is highly flexible, as it allows you to work with different file types (e.g. text files, binary files, CSV files, etc.), and to perform different operations on files (e.g. read, write, append, etc.).
User–friendly: Python provides a user-friendly interface for file handling, making it easy to create, read, and manipulate files.
Cross-platform: Python file-handling functions work across different platforms (e.g. Windows, Mac, Linux), allowing for seamless integration and compatibility.
Disadvantages of File Handling in Python
Error-prone: File handling operations in Python can be prone to errors, especially if the code is not carefully written or if there are issues with the file system (e.g. file permissions, file locks, etc.).
Security risks: File handling in Python can also pose security risks, especially if the program accepts user input that can be used to access or modify sensitive files on the system.
Complexity: File handling in Python can be complex, especially when working with more advanced file formats or operations. Careful attention must be paid to the code to ensure that files are handled properly and securely.
Performance: File handling operations in Python can be slower than other programming languages, especially when dealing with large files or performing complex operations.
Why there is a need of File Handling?
- File helps us to store the data permanently, which can retrieve for future use. Hence, a file operation can be done in the following order.
- Open a file that return a file handle
- Read or write – (use handle to Performing operation)
- Close the file handle
Note: File is a named location on disk to store related information
- Type of Files: There are mainly two type of files in python these are:
1 ) Text Files 2) Binary Files
Text Files : It stores information in ASCII or Unicode characters . Each line of text is terminated (delimited) with a special character known as EOL (End of Line). In python by default this EOL character is the newline character (“\”) or carriage return, newline combination (“\r\n”)
Binary File: Contains information in the same format in which the information is held in memory. There is no delimiter for a line. No translations are required and more secure Exam: Image file , audio file, exe file etc
Deference between Text files and Binary Files
|
Text Files stores information in ASCCI characters |
Binary Files are used to store binary data such as images , audio , video and text |
|
Each line of text is terminated with a special character known as EOL (End of Line) |
There is no delimiter in Binary File |
|
Text files are easy to understand because these files are in Human readable form. |
Binary files are difficult to understand |
|
Text files are slower than binary files |
Binary files are faster and easier for a program to read and write of text files |
|
Text Files are having extension .txt |
Binary files are having extension .bat |