Notes on File Handling in Python
2
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.9876
takes 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])
: Readsn
bytes or the entire file ifn
is omitted.file_object.readline([n])
: Reads a line orn
bytes.file_object.readlines()
: Reads all lines into a list.
- Write:
file_object.write(str)
: Writes stringstr
to 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
r
before 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)