REVISION SUMMARY: File Handling in Python (NCERT Class 12)
open() function creates a file object (file handle) that links the program to the file for read/write operations; the file must be closed after use to free resources.write() (single string) or writelines() (sequence of strings) and read using read(), readline() or readlines().tell() returns the current byte position of the file object; seek() repositions it for random access using offset and reference point (0 = beginning, 1 = current, 2 = end).pickle module handles binary files by pickling (serializing) Python objects with dump() and unpickling (de-serializing) them with load().\n in Python); stored with extensions such as .txt, .py, .csv.\n); when encountered, the remaining contents are displayed from a new line.open() that establishes a link between the program and the data file; used to call read/write functions.open() that specifies the operation (r = read, w = write, a = append, + = read+write, b = binary); default is read mode in text.python
file_object = open(file_name, access_mode)
file_object.close()
Example: myObject = open("myfile.txt", "a+"); myObject.close()
with clause (auto-closes file)
python
with open(file_name, access_mode) as file_object:
Example: with open("myfile.txt","r+") as myObject: content = myObject.read()
write() method
python
file_object.write(string)
Example: myobject.write("Hey I have started using files in Python\n")
writelines() method
python
file_object.writelines(iterable_of_strings)
Example: myobject.writelines(["Hello everyone\n", "Writing multiline strings\n"])
read() method
python
file_object.read([n])
Example: myobject.read(10)
readline() method
python
file_object.readline([n])
Example: myobject.readline(10)
readlines() method
python
file_object.readlines()
Example: myobject.readlines()
tell() method
python
file_object.tell()
Example: fileobject.tell()
seek() method
python
file_object.seek(offset [, reference_point])
Example: fileobject.seek(10) # reference_point defaults to 0
pickle.dump() and pickle.load()
python
pickle.dump(data_object, file_object)
store_object = pickle.load(file_object)
Example: pickle.dump(listvalues, fileobject); objectvar = pickle.load(fileobject)
Writing data to a text file (write/append mode)
- Open file in 'w' (overwrites) or 'a' (appends) mode.
- Accept string(s) from user; convert numeric data to string with str().
- Call write(string + '\n') or writelines(list_of_strings).
- Close the file (or use with).
Reading a text file line-by-line (looping over file object)
- Open in 'r', 'r+', 'w+' or 'a+' mode.
- Use readline() inside a while loop: str = fileobject.readline(); while str: print(str); str = fileobject.readline().
- Stop when readline() returns empty string (EOF reached).
Random access using tell() and seek()
- Call tell() to obtain current byte position.
- Call seek(offset, 0/1/2) to move file object (0 = start, 1 = current, 2 = end).
- Read from new position with read().
Pickling / unpickling records (binary file)
- Import pickle.
- Open in 'wb'/'ab' for dump or 'rb' for load.
- pickle.dump(list_or_object, fileobject) to store; pickle.load(fileobject) to retrieve.
- Handle EOFError with try-except when reading multiple records.
write() without str() conversion.\n at end of strings written with write() (lines not separated).readlines() and expecting a single string instead of a list of strings ending with \n.seek() (defaults to 0; students often forget 0/1/2 meanings).pickle before using dump()/load() on binary files.writelines() returns number of characters written (only write() does).A study aid reviewed by GFIS faculty — always verify with your textbook and teacher.