File Handling
File Handling
Contents
1. Contents.....................................................................................................1
1. Definitions:.................................................................................................2
2. Types of files:.............................................................................................2
2.1 Text Files – Stores information in the form of a stream of ASCII Code.. 2
2.1.1 Regular text files – Normal text files....................................................2
2.1.2 – Tab Separated values (TSV) – Tab separated values are stored in the
file.................................................................................................................2
2.1.3 – Comma Separated Values (CSV) – Comma separated values are
stored in file..................................................................................................2
2.2 Binary File – A binary file stores the information in the form of a
stream of bytes.............................................................................................2
3. Open Command.........................................................................................2
4. 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
2. Short Cuts to reading files..........................................................................6
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.
2 File Object – 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.
2.1.1 Regular text files
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
1 read() : This function reads the entire file and returns a string
2 read(n) : This function reads n bytes from the file and returns a
string
3 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.
4 readlines() : This function returns a list where each element is
single line of that file.
4. Write Commands
a. write() : This function writes a fixed sequence of characters
to a file.
b. writelines() : This function writes a list of string.
5. 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.
Fd is file descriptor
Read mode- file not present eroor , write mode , append will make
file if its nt there
Note :
X=fd.read() reads entir line
X=fd.read(10) reads 10 bytes
C=fd.readline(0 gives abc/n……
X=fd.readlines() reads everyting
Offset tells kitne aage jana hai , ref tells kahan se jana hai
Ref-0=beg of file , 1 =surrent post , 2= end off file use din csu and
binary files not text files