Introduction to File Handling
- File Handling: Allows reading from and writing to disk files in Python. It enables data storage beyond program execution.
Data Files
- Text Files:
- Store data as ASCII or Unicode characters.
- Example:
"computer"takes 8 bytes,11237.9876takes 10 bytes. - End-of-Line (EOL) characters:
'\n'or'\r'.
- Binary Files:
- Store data in its raw memory format (e.g., integers, floats).
- No EOL delimiters.
- Faster and more efficient but requires specific reading through Python.
Steps in Data File Handling
- Opening Files:
- Syntax:
file_object = open(filename, mode) - Default mode:
'r'(read). Other modes include'w'(write),'a'(append).
- Syntax:
- Performing Read/Write Operations:
- Read:
file_object.read([n]): Readsnbytes or the entire file ifnis omitted.file_object.readline([n]): Reads a line ornbytes.file_object.readlines(): Reads all lines into a list.
- Write:
file_object.write(str): Writes stringstrto the file.file_object.writelines(list): Writes a list of strings to the file.
- Read:
- Closing Files:
- Syntax:
file_object.close() - Ensures data is saved and file resources are released.
- Syntax:
File Modes and Paths
- Opening File Modes:
'r': Read (default).'w': Write (overwrites file).'a': Append (adds to the end).
- File Path Syntax:
- Absolute Path: Full path from the root, e.g.,
C:\SALES\2018\REVENUE.TXT. - Relative Path: Relative to the current directory, e.g.,
.\2019\SHEET.XLS. - Double Backslash (\): Used in paths to escape the backslash, e.g.,
d:\\mydata\\poem.txt. - Raw Strings: Use
rbefore the string to treat backslashes literally, e.g.,r"d:\mydata\poem.txt".
- Absolute Path: Full path from the root, e.g.,
File Operations
- File Handle: The reference to the file used for reading/writing operations.
- Flush:
file_object.flush()forces the buffer to write to the file. - Stripping Whitespace: Use
.strip(),.lstrip(), or.rstrip()to remove whitespace from strings.
Standard Input, Output, and Error
- Standard Input (stdin): Reads from the keyboard.
- Standard Output (stdout): Writes to the monitor.
- Standard Error (stderr): Writes errors to the monitor.
- Using sys module:pythonCopy code
import sys sys.stdin, sys.stdout, sys.stderr
- Using sys module:pythonCopy code
Using with Statement
- Context Manager:
- Syntax:
with open(filename, mode) as file_handle: # file operations - Automatically closes the file after the block of code.
- Syntax:
Binary File Operations
- Pickle Module:
- Dump:
pickle.dump(object, file_handle)writes an object to a binary file. - Load:
pickle.load(file_handle)reads an object from a binary file.
- Dump:
Current Working Directory
- Getting Current Directory:
import os pwd = os.getcwd() print("Current Directory:", pwd)
Read this also:
Related News
बांग्लादेश ए महिला बनाम पाकिस्तान ए महिला, सेमीफाइनल 2 (बी1वीएसए2), एएसी महिला एशिया कप राइजिंग स्टार्स 2026 – पूर्ण कमेंट्री
बाबर आजम: पाकिस्तान की टी20 टीम में उनकी भूमिका और भविष्य की योजनाएं
आर रहमान ने सुप्रीम कोर्ट में दागर ब्रदर्स के ‘शिव स्तुति’ प्रदर्शन को स्वीकार करने के लिए सहमति व्यक्त की
ब्रह्मांड की पहली गलतियों में से एक का पता लगाया गया है
ब्लैक होल्स: विंड्स या जेट्स, दोनों नहीं
यात्रा से विकसित होने वाली संयम और लचीलापन की क्षमता
