File Handling Introduction
File Handling Introduction
A file is a
resource to store data. As part of the programming requirement, we may have to store our data permanently for future
purpose. Files are very common permanent storage areas to store our data.
Python File Handling
Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file
handling options, to operate on files. The concept of file handling has stretched over various other languages, but the
implementation is either complicated or lengthy, like other concepts of Python, this concept here is also easy and short.
Advantages of File Handling in Python
Versatility: File handling in Python allows you to perform a wide range of operations
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
Cross-platform: Python file-handling functions work across different platforms
Disadvantages of File Handling in Python
Error-prone: File handling operations in Python can be prone(tendency or liable) to errors
Security risks: File handling in Python can also pose security risks
Complexity: File handling can be complex, especially when working with more advanced file formats or operations
Performance: File handling operations in Python can be slower than other programming languages
File Types
Text files: Text files are those which store data in the form of characters or strings. A file whose contents can be
viewed using a text editor is called a text file. A text file is simply a sequence of ASCII or Unicode characters. Python
programs, contents written in text editors are some of the example of text files
Binary files: Binary files are those which store entire data in the form of bytes, i.e a group of 8 bits each. While
retrieving data from the binary file, the programmer can retrieve it in the form of bytes. Binary files can be used to
store text, images, audio and video.
CSV (Comma-Separated Values) is one of the prevalent and accessible file formats for storing and exchanging
tabular data. CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a
spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data
record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator
is the source of the name for this file format. For working CSV files in Python, there is an inbuilt module called CSV.