Chapter 5.1 Text File Handling
Chapter 5.1 Text File Handling
2
1. Types of files:....................................................................................................... 2
2.1 Text Files........................................................................................................ 2
2.2 Binary File...................................................................................................... 2
2.3 Comma Separated Values (CSV)....................................................................2
2. Open Command................................................................................................... 2
2. Read Commands.................................................................................................. 4
4. Write Commands.................................................................................................. 4
5. Seek Command.................................................................................................... 4
6. Flush Command - fd.flush().................................................................................. 4
7. Close Command - fd.close()................................................................................. 4
8. With command syntax and usage........................................................................5
9. Short Cuts to reading files.................................................................................... 5
File Handling
1. Definitions:
1 Relative and Absolute Paths.
i. Relative path – The file name mentions the path relative to
the current working directory.
ii. Absolute Path – The file name mentions the complete path
from the top-level directory. The file name starts with the
drive name. (C: , D:)
2 File Object, File Handler , File Descriptor – A file object is a
reference to a file on disk. It opens and makes it available for a
number different task.
3 Offset - The position where the file reader is currently pointing.
2. Types of files:
2.1 Text Files
Stores information in the form of a stream of ASCII Code.
3. Open Command
fd = open (file_name,mode)
open() Parameters
The open() function returns a file object which can used to read, write
and modify the file.
If the file is not found, it raises the FileNotFoundError exception.
4. Read Commands
4 read() : This function reads the entire file and returns a string
5 read(n) : This function reads n bytes from the file and returns a
string.
6 readline() : This function reads lines from that file and returns
as a string. It will fetch the line n, if it is been called nth time.
To remove the ‘\n’ at the end of the line call:-
line = fh.readline()
line = line.rstrip(‘\n’)
7 readlines() : This function returns a list where each element is
single line of that file.
5. Write Commands
a. write() : This function writes a fixed sequence of characters
to a file. To go to the next line after writing put a ‘/n’ at the
end of the write string.
b. writelines() : This function writes a list of string.
6. Tell Command
fd.tell()
It returns the current offset of the pointer in the file.
7. Seek Command
fd.seek(offset, reference)
- Offset : no of bytes.
- Reference – 0: Beginning of the file.
1: Current position of the file.
2: End of the file.
import sys
fd = open(r”E:\work.txt”)
line1= fd.readline()
line2= fd.readline()