File Handling 1
File Handling 1
Files
• Files are named locations on disk to store related information. They are
used to permanently store data in a non-volatile memory (e.g. hard disk).
• Since Random Access Memory (RAM) is volatile (which loses its data
when the computer is turned off), we use files for future use of the data
by permanently storing them.
1. Binary file
2. Text file
Binary files in Python
• All binary files follow a specific format. We can open some binary files in the
normal text editor but we can’t read the content present inside the file. That’s because
all the binary files will be encoded in the binary format, which can be
understood only by a computer or machine.
• For handling such binary files we need a specific type of software to open it.
• Most of the files that we see in our computer system are called binary
files.
• Example:
• 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.
Text files in Python
• The method open() is used to open an existing file or creating a new file. If the complete
directory is not given then the file will be created in the directory in which the python file is
stored. The syntax for using open() method is given below.
– Syntax:
– file_object = open( “Access Mode”, “”)
• The open method returns file object which can be stored in the name file_object (file-
handle).
• File name is a unique name in a directory. The open() function will create the file with
the specified name if it is not already exists otherwise it will open the already existing
file.
Opening Files in Python
• The access mode
it is the string which tells in what mode the file should be opened for operations. There are
different access modes are available in python.
• Reading: Reading mode is crated only for reading the file. The pointer will be at the
beginning of the file.
• Writing: Writing mode is used for overwriting the information on existing file.
• ‘r' – Read Mode: Read mode is used only to read data from the file.
• ‘w' – Write Mode: This mode is used when you want to write data into the file or modify it.
Remember write mode overwrites the data present in the file.
• ‘a' – Append Mode: Append mode is used to append data to the file. Remember data
will be appended at the end of the file pointer.
• ‘r+' – Read or Write Mode: This mode is used when we want to write or read the data from
the same file.
• ‘a+' – Append or Read Mode: This mode is used when we want to read data from the file
or append the data into the same file.
Opening Files in Python (cont…2)
• ‘wb’ – Open a file for write only mode in the binary format.
• ‘rb’ – Open a file for the read-only mode in the binary format.
• ‘ab’ – Open a file for appending only mode in the binary format.
• ‘rb+’ – Open a file for read and write only mode in the binary format.
• ‘ab+’ – Open a file for appending and read-only mode in the binary
format.
Reading Information in the File
• In order to read a file in python, we must open
the file in read mode.
• There are three ways in which we can read the
files in python.
– read([n])
– readline([n])
– readlines() – all lines returned to a list
• Here, n is the number of bytes to be read.
Feature read() readline() readlines()
What it reads Entire file or size bytes One line All lines
Memory usage High (for large files) Low High (for large files)
Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “r”)
print(my_file.read(5))
Output:
Hello
– write(string)
– writelines(list)
• Example 1:
my_file = open(“C:/Documents/Python/test.txt”, “w”) my_file.write(“Hello
World”)
The above code writes the String ‘Hello World’ into the ‘test.txt’ file.
• The first line will be ‘Hello World’ and as we have mentioned \n character, the
cursor will move to the next line of the file and then write ‘Hello Python’.
• Remember if we don’t mention \n character, then the data will be written
continuously in the text file like ‘Hello WorldHelloPython’
Example 3:
fruits = *“Apple\n”, “Orange\n”, “Grapes\n”, “Watermelon”+
my_file = open(“C:/Documents/Python/test.txt”, “w”)
my_file.writelines(fruits)
The above code writes a list of data into the ‘test.txt’ file simultaneously.
File Pointer Manipulation with seek() in Python
In Python, the seek() function allows you to control the file pointer's position within a file.
This is useful for tasks like: