FILE HANDLING
CHAPTER 5
DATA FILES
The data files are the files that stores data pertaining to a specific application. The data files can be stored in two ways:
TEXT FILES
BINARY FILES
OPERATIONS IN A FILE
OPENING FILES
OPENING FILES
OPENING FILES
Important Note:
Important Note:
Important Note:
Important Note:
File Object / File Handle
Absolute & Relative Path
Absolute paths
Relative paths
.\TWO.CPP
..\CL.DAT
..\PROJ1\REPORT.PRG
Standard input, output and error streams
Flush function
f=open(‘CD.txt’,’a’)
f.write(‘Hello all’)
f.flush()
Python holds everything to write in the file in buffer and pushes it onto the actual file at a later time. If you want to force python to write the contents of buffer onto the file immediately use the flush function.
File Access Modes
Text File Mode | Binary File Mode | Description | Notes |
‘r’ | ‘rb’ | Read only |
|
‘w’ | ‘wb’ | Write only |
|
‘a’ | ‘ab’ | Append |
|
File Access Modes
Text File Mode | Binary File Mode | Description | Notes |
‘r+’ | ‘r+b’ or ‘rb+’ | Read and Write |
|
‘w+’ | ‘w+b’ or ‘wb+’ | Write and Read |
|
‘a+’ | ‘a+b’ or ‘ab+’ | Write and Read |
|
CLOSING A FILE
Writing into the file
Reading from the file
Reading functions from the file
var = <file_handle>.read(n)
It reads n bytes , if no n is specified, reads the entire file
If the file contains the following data :
Reading functions from the file
var = <file_handle>.readline(n)
Reads a line of input, if n is specified reads at most the n bytes
Returns the read bytes in the form of a string ending with \n or returns a blank string if no more bytes are left.
Reading functions from the file
var = <file_handle>.readlines()
Reads all the lines and returns them in a list.
Reading the complete file line by line
Output
Using rstrip
Reading the complete file line by line
Using with for Files�
same as
Reading the complete file Character by character
Output
Writing into a file
Write() - writes the string to the file
Writelines() writes all the strings in the list to the file
Relative and Absolute paths
Absolute path name
If you want the absolute path name of the file Bank.act under account it will be
E:\Accounts\Bank.act
Relative path name
Relative path name are relative to current working directory denoted by a dot and the path directory denoted by 2 dots
If you are in folder PROJ2
To access TWO.CPP we can write
.\TWO.CPP (Proj2 being the current folder)
If you want to access PROJ1 then we can write
..\PROJ1 (Proj 2 being the current folder)
Removing White spaces after reading from a file.
PROGRAMS – Using functions
PROGRAMS – Using functions
Write a menu driven program to perform the following operations ( File name: Poem.txt)
Methods of OS module
Syntax : os.rename(current_file_name, new_file_name)
2. The remove() method to delete file.
Syntax : os.remove(file_name)
Syntax: os.mkdir("newdir")
.
Methods of OS module
4. The chdir() method to change the current directory. Syntax: os.chdir("newdir")
5. The getcwd() method displays the current directory. Syntax: os.getcwd()
6. The rmdir() method deletes the directory.
Syntax os.rmdir('dirname')
File object attributes
Getting & Resetting the Files Position
The tell() method of python return backs the current position within the file.
The seek(offset)
The offset argument indicates the number of bytes to be moved.
seek() method�
To read or write at a specific position, use the seek() function to set the current read/write position.
f.seek(from,offset)
Here, the offset parameter takes the following values:
BINARY FILE
Reading and Writing to a Binary File�
BINARY FILES OPERATIONS Most of the files that we see in our computer system are called binary files.
Example:
• Document files: .pdf, .doc, .xls etc.
• Image files: .png, .jpg, .gif, .bmp etc.
• Video files: .mp4, .3gp, .mkv, .avi etc.
• Audio files: .mp3, .wav, .mka, .aac etc.
• Database files: .mdb, .accde, .frm, .sqlite etc.
• Archive files: .zip, .rar, .iso, .7z etc.
• Executable files: .exe, .dll, .class etc
Python Pickle module
To write and read from a Binary File
Write into the file
Read from the file
Major operations performed using a binary file