File-Handling in Python
File-Handling in Python
- Text File: stores data as text using ASCII or Unicode character coding scheme. Each line
in file ends with newline character and each file ends with EOF character.
- Binary File: stores data in binary form, and is intended for a program to read. We cannot
view its contents with a text editor. Binary file also ends with EOF character.
We shall be working with text files in our context. Let’s first take few examples:
print(file_data)
Opening the File: We open a file using file opening instruction, whose syntax is:
Here,
If the file is present in the same path as the path of python or the IDE that we
are working on, then we need not provide any path and just mentioning
filename will suffice! OR, if the file is present in any directory (already
existing) of that path, then we just need to specify that directory name
followed by filename. For example: records.txt exists in myFiles directory
inside spyder’s path, then we can access this file using: myFiles\records.txt –
we need not start from root C:\ - this is referred to as relative filename
(relative to the current path, which is spyder’s path here)
file_variable_name - is the corresponding file object for filename that we’ll be using
in the program to refer to the filename.
mode - is a string that specifies the mode in which file will be opened. Python
has following file-opening modes:
“r” : opens a file for reading only (no writing is possible!). This is
the default mode for opening a file – if no mode is specified, ‘r’ is
assumed by default. The file pointer is placed at the beginning of the
file.
“w” : opens a file for writing – if the file is already existing, its
contents will get erased. If the file doesn’t exist, it gets created!
“a” : opens the file in append mode – if the file exists, new data will
be written to the end of file as the file pointer is placed at the end of the
file. If the file doesn’t exist, it gets created!
“ r+ ” : opens the file for both reading and writing. The file pointer is
placed at the beginning of the file.
We can append “t” or “b” along with modes to indicate the type of file we’ll be working with.
For example – if we specify mode as “rb” then it indicates we are opening a binary file in
read mode. If neither of these specified, “t” or text file is assumed by default!
file_variable_name.close()
Although, the file is automatically closed when the program ends but it is still a good practice
to do so explicitly. Failing to close the file in a large program could be problematic and may
even cause the program to crash because it will keep consuming the resources associated.
Writing Data to a File: There are two methods for writing data to a file:
file_variable_name.write(string)
Example: outFile.write(“Hello!!”)
outFile.write(“and welcome!!”)
will write strings – Hello!! And welcome!! on the same line in the file referred to by
variable name, outFile.
If we want these two string to be written to different lines (new lines) in the file, we’ll
need to specify newline character(“\n”) after the string as:
outFile.write(“Hello!!\n”)
outFile.write(“and welcome!!”)
Hello!!
and welcome!!
file_variable_name.write(sequence)
Example:
lines = ["Hello World!\n", "Welcome to Python\n", "Python is fun"]
outFile.writelines(lines)
outFile.close()
file_variable_name.read()
file_data = infile.read()
USA
UK
Canada
2) readline() - reads the single line of the string from the sequence written to the file,
Syntax:
file_variable_name.readline()
Example: reading the file that has ‘lines’ sequence written to it using:
firstline = infile.readline()
print(firstline)
secondline = infile.readline()
print(secondline)
Hello World!
Welcome to Python
NOTE:
Hello World!
Welcome to Python
3) readlines() - reads all the lines from the sequence written to the file and returns the
list of the lines from the file, syntax:
file_variable_name.readlines()
Example: reading the file that has ‘lines’ sequence written to it using:
line_seq = infile.readlines()
print(line_seq)
File-object attributes:
Exercise – 1: Write a program that reads a file line by line and finds the count of vowels
present on the input text file.