File Handling
File Handling in Python - Text file Manipulation
Introduction
- Python programs are transient: they take input, process it, and produce output.
- Processed data disappears once the program finishes execution.
- Next execution requires the user to provide data again.
- Data is stored in primary memory (RAM), which is volatile (temporary).
The Need for Data Files
- Processed data may be required for later use (e.g., exam results for annual reports).
- Data files store processed data permanently in secondary memory.
Data Files
- A data file is a sequence of related data, stored permanently in a computer's secondary memory (e.g., hard disk).
- It has a name for identification.
Why Use Files?
- Convenient for manipulating large quantities of data.
- Allows permanent data storage (until deletion).
- Avoids retyping data multiple times.
- Enables data sharing between programs.
Properties of Files
- Every file has two key properties:
Filename
- The file name is used to uniquely identify a file stored in a computer's file system.
File Path
- The file path specifies the location of a file on the computer.
- It can be represented as:
- Absolute Path
- Relative Path
Absolute Path
- Describes how to access a file or folder, starting from the root of the file system.
- Begins with a drive letter followed by a colon (e.g., C: or E:), then folders and subfolders, and ends with the file name.
- Example:
C:\Desktop\myfile\text.txt - In Python, slashes () in the path are doubled because the slash () is used as an escape sequence.
Relative Path
- Interpreted from the perspective of the current working folder.
- Only the file name is given without the drive letter, slashes, and folders.
- The file name is automatically joined with the current folder to create an absolute file path.
- Example: If the current location is
C:\Python, using text.txt will result in the path C:\Python\text.txt.
Types of Data Files
- Based on how data is stored, files are divided into two categories:
Text File
- A sequence of characters consisting of alphabets, numbers, and special symbols.
- Examples:
.txt, .py, .csv. - Data is stored in the form of a stream of ASCII or UNICODE characters.
- Internally stored as a sequence of bytes (0s and 1s).
- Each character is converted to bytes using ASCII, UNICODE, or other encoding schemes.
- Text editors translate ASCII values to readable characters.
- ASCII is the default character coding format in Python.
- Using the constant "u" before the string, data can be written in UNICODE format.
- Each line ends with a special character called EOL (end of line character).
- In Python, EOL characters are newline (
\n) or Carriage Return (\r). - Text files can be created and manipulated through any text editor (e.g., notepad).
Binary File
- Stores data in the same format as stored in memory.
- Not readable when opened in a text editor.
- Encoded in binary format.
- Can only be read and manipulated in the programming language used to create it.
- No delimiter for a line.
- Faster to read and write due to no translations.
Text Files Manipulations
- File manipulation involves three steps:
- Opening the file
- Processing data (writing, reading, editing)
- Closing the file
- Order: Open -> Process -> Close
File Object of Python
- A file handle or file object is a stream of bytes used to read or write data.
- Used to write processed data from the program to a file or read data from a file to be manipulated.
- Establishes a link between the program and the data file in permanent storage.
Opening Files Using open()
- Opening a file prepares it for reading or writing.
- File manipulation in Python is done using a file object, created with the built-in
open() function. - The
open() function returns a file object used for accessing various methods and functions for file manipulation. - Syntax:
fileobject=open("filename","Accessmode") open() takes two arguments and returns a file object.- The file object is a reference to a file on disk, assigned to an identifier for performing operations.
file_name is the name of the file to be opened.- If only the file name is specified without the path, Python searches/creates the file in the current folder (relative path).
- If the file is not in the current folder, the absolute path is required.
- The file extension is not mandatory, but
.txt is commonly used for text files. - Access modes govern the type of operations possible.
- They also define the location of the File Object (file handle) in the file (cursor position).
- Access mode is an optional parameter, with the default being `