File
A file is a named location on a secondary storage media where data are permanently stored for later access.
Computers store every file as a collection of 0s and 1s i.e., in binary form. Therefore, every file is basically just a series of bytes stored one after the other. There are mainly two types of data files — text file and binary file. A text file consists of human readable characters, which can be opened by any text editor. On the other hand, binary files are made up of non-human readable characters and symbols, which require specific programs to access its contents.
Text Files:
A text file is usually considered as sequence of lines. Line is a sequence of characters (ASCII) or UNICODE characters , stored on permanent storage media. In Text File each line is terminated by a special character, known as End of Line (EOL). From strings we know that \n is newline character.
By default the EOL will be '\n' or it may be \t,\r etc.
So at the lowest level, text file will be collection of bytes. Text files are stored in human readable form and they can also be created using any text editor.
A text file stores data in the form of alphabets, digits and other special symbols by storing their ASCII values and are in a human readable format For example, any file with a .txt, .c, ,py, .ini ,.rtf etc.
The text files can be of two types :
Regular text files : store text in the same form as typed. Every line terminated by a special character EOL as \n. These files have a file extension as .txt
Delimited Text files: A special character is stored to separate the values. when comma(,) used files called CSV files , when Tab space(\t) is used files called TSV (tab separated values)
Binary File
Binary files are also stored in terms of bytes (0s and 1s), but unlike text files, these bytes do not represent the ASCII or Unicode values of characters. Rather, they represent the actual content such as image, audio, video, compressed versions of other files, executable files, etc. These files are not human readable. Thus, trying to open a binary file using a text editor will show some garbage values. We need specific software to read or write the contents of a binary file.
A small error in a textual file can be recognized and eliminated when seen. Whereas, a small error in a binary file corrupts the file and is not easy to detect.
Since the data is not human readable it also adds to the security of the content as one might not be able to get data if the structure is not known.
Now, when it comes to programming there are three major differences between the two, i.e., Handling of newlines, storage of numbers and representation of EOF(End of File) is complex compare to .
File Handling :
includes functions related to
Creating files
Reading file contents
Writing file contents
Appending data in files
Open and close files
operations using python programming.
File handling steps
Open a file
Read or write or append (perform operation)
Close the file
Opening Files in Python
open () function in Python to open a file in read or write mode
syntax
open(filename, mode).
There are three kinds of mode, that Python provides and how files can be opened:
“ r “, for reading.
“ w “, for writing.
“ a “, for appending.
“ r+ “, for both reading and writing
"a+" for append and read
Opening a file using with clause
In Python, we can also open a file using with clause.
The syntax of with clause is:
with open (file_name, access_mode) as file_object
The advantage of using with clause is that any file that is opened using this clause is closed automatically, once the control comes outside the with clause.
with open(“myfile.txt”,”r”) as myObject:
content = myObject.read()
with open('readme.txt') as f:
lines = f.readlines()
Reading Writing text files
There are two ways to write in a file.
write() : Inserts the string s in a single line in the text file.
f.write(s)
writelines() : For a list of string elements, each string is inserted in the text file.Used to insert multiple strings at a single time.
L = [str1, str2, str3]
f.write(l)
Reading from a file
There are three ways to read data from a text file.
read() : Returns the read n bytes in form of a string. Reads n bytes, if no n specified, reads the entire file.
f.read(n)
readline() : Reads a line of the file and returns in form of a string.For specified n, reads at most n bytes. However, does not reads more than one line, even if n exceeds the length of the line.
f.readline(n) # read n characters from line
f.readline() # read entire line
readlines() : return list of strings. Reads all the lines and return them as each line a string element in a list.
f.readlines()
tell() method
Is used to tell the position of file pointer(file object/ fill handle) for read write operation.
File handle is like a cursor, which defines from where the data has to be read or written in the file.
seek( ) method
Is used to move the the position of file pointer (file object/file handle) to the given byte number.
file_object.seek(offset , reference_point)
The reference point is selected by the from_what argument. It accepts three values:
0: sets the reference point at the beginning of the file
1: sets the reference point at the current file position
2: sets the reference point at the end of the file
By default from_what argument is set to 0
1 and 2 works only in binary file modes
Example
>>> f=open("abc.txt","w")
>>> f.write("12345678910")
>>> f=open("abc.txt","r")
>>> f.read()
'12345678910'
>>> f.read() # file pointer at end of file
''
>>> f.seek(5,0) # file pointer at 5th byte from starting
5
>>> f.read()
'678910'
>>> f.tell()
11
>>> f.seek(1,0)
1
>>> f.read()
'2345678910'
Use of 1 and 2 reference point
Create a file to write contents in a binary file
f=open("abc.dat","w")
s="1234567890"
f.write(s)
f.close()
now open python prompt and write code for backward as well as forward move using seek( )
>>> f=open("abc.dat","rb")
>>> f.seek(0,1)
0
>>> f.read()
b'1234567890'
>>> f.seek(-2,2)
8
>>> f.read()
b'90'
>>> f.seek(-2,1)
8
>>> f.read()
b'90'
Definition of open modes r, r+, w, w, a, a+:
The r throws an error if the file does not exist or opens an existing file without truncating it for reading; the file pointer position at the beginning of the file.
The r+ throws an error if the file does not exist or opens an existing file without truncating it for reading and writing; the file pointer position at the beginning of the file.
The w creates a new file or truncates an existing file, then opens it for writing; the file pointer position at the beginning of the file.
The w+ creates a new file or truncates an existing file, then opens it for reading and writing; the file pointer position at the beginning of the file.
The a creates a new file or opens an existing file for writing; the file pointer position at the end of the file.
The a+ creates a new file or opens an existing file for reading and writing, and the file pointer position at the end of the file.
What is + means in open()?
The + adds either reading or writing to an existing open mode, aka update mode.
The r means reading file; r+ means reading and writing the file.
The w means writing file; w+ means reading and writing the file.
The a means writing file, append mode; a+ means reading and writing file, append mode.
Read a file with r
with open('file.txt') as f: # default `r` mode
print(f.read())
output put
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
In r mode, if we write the file, Python throws io.UnsupportedOperation: not writable
with open('file.txt', 'r') as f: # default `r` mode
f.write("test \n") # throws UnsupportedOperation
Read and write a file with r+
In r+ mode, we can read and write the file, but the file pointer position is at the beginning of the file; if we write the file directly, it will overwrite the beginning content.
See the below example:
with open('file.txt', 'r+') as f:
f.write("new line \n")
output
new line
python 1
welcome to python 2
welcome to python 3
welcome to python 4
Difference between r+ and w+ in open()
Below is the difference between r+ and w+:
If the file does not exist, r+ throws FileNotFoundError; the w+ creates the file.
If the file exists, r+ opens it without truncating; the w+ truncates the file and opens it.
Difference between r+ and a+ in open()
Below is the difference between r+ and a+:
If the file does not exist, r+ throws FileNotFoundError; the a+ creates the file.
For r+ mode, the initial file pointer position at the beginning of the file; For a+ mode, the initial file pointer position at the end of the file.
Difference between w+ and a+ in open()
Below is the difference between w+ and a+:
If the file exists, w+ truncates the file and opens it; a+ opens it without truncating.
For w+ mode, the initial file pointer position at the beginning of the file; For a+ mode, the initial file pointer position at the end of the file.
Difference between a and a+ in open()
The a+ adds reading file to the existing a mode.
A text file for testing.
file.txt
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
Append a file with a
with open('file.txt', 'a') as f:
f.write("4")
Output
file.txt
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
4
Append a file with a+
Open a file for reading and writing with a+.
Counts the number of the lines.
Append the result to the file.
with open('file.txt', 'a+') as f:
f.seek(0) # file pointer at end, move to beginning
lines = f.readlines() # read all and file pointer at end again
f.write("\n" + str(len(lines))) # append number of lines to a file
Output
file.txt
welcome to python 1
welcome to python 2
welcome to python 3
welcome to python 4
4
File flush() method
The flush() method in Python file handling clears the internal buffer of the file. In Python, files are automatically flushed while closing them. However, a programmer can flush a file before closing it by using the flush() method.
fileObject.flush()
This method does not require any parameters and it does not return anything.
#file abc.txt contains text I love my India
# opening the file in read mode
f = open("gfg.txt", "r")
# clearing the input buffer
f.flush()
# reading the content of the file
fileContent = f.read()
# displaying the content of the file
print(fileContent)
# closing the file
f.close()
output :
I love my India
In the above program, the abc.txt is opened in read mode then the flush() method only clears the internal buffer of the file, it does not affect the content of the file. So, the contents of the file can be read and displayed.
Another Example
# program to demonstrate the use of flush()
# creating a file
f= open("abc.txt", "w+")
# writing into the file
f.write("I love My India")
# closing the file
f.close()
# opening the file to read its content
f = open("abc.txt", "r")
# reading the contents before flush()
fileContent = f.read()
# displaying the contents
print("\nBefore flush():\n", fileContent)
# clearing the input buffer
f.flush()
# reading the contents after flush()
# reads nothing as the internal buffer is cleared
fileContent = f.read()
# displaying the contents
print("\nAfter flush():\n", fileContent)
# closing the file
f.close()
Output:
Before flush():
I love my India
After flush():
In this program initially, we create abc.txt file and write I love my India as content in it and then we close the file. After that we read and display the contents of the file and then the flush() method is called which clears the input buffer of the file so the fileObject reads nothing and fileContent remains an empty variable. Hence nothing is displayed after flush() method.