We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels.
Python Interview Questions on File Manipulation
Till now you have learned how to implement logic in Python to write blocks of code that can help you accomplish certain tasks. In this section, we will learn about how to use Python to work with files. You can read data from files and you can also write data to the files. You can not only access the internet but also check your emails and social media accounts using the Python programming language.
A file has a permanent location. It exists somewhere on the computer disk and can be referred to anytime. It is stored on the hard disk in non-volatile memory which means the information in the file remains even if the computer is switched off. In this section, we will learn how to deal with files in Python.
Open a File
If you want to work on an existing file, then you will have to first open the file. In this section, we will learn how to open a file.
In order to open a file, we will make use of the open( ) function which is an inbuilt function. When we use the open( ) function, a file object is returned which can be used to read or modify the file. If the file exists in the same directory where python is installed then you need not give the entire pathname. However, if the location is different then you will have to mention the entire path.
For this example, I have created a file by the name: learning_files.txt in the current directory of python /home/pi.
The content of the file is as follows:
I am great a learning files
See how Good I am at opening Files
Thank you Python
Look at the following piece of code:
>>> f_handle = open ("learning_files . txt") >>> print (f_handle . read ( ) )
In case the file is not available, the error message will be displayed as follows:
>>> f_handle = open ("llllearning_files . txt") Traceback (most recent call last): File "<pyshell#0>", line 1, in <module> f_handle = open ("llllearning_files . txt") FileNotFoundError: [Errno 2] No such file or directory: ' llllearning_files.txt' >>>
I am great a learning files See how Good I am at opening Files Thank you Python
Python File Modes
Python has defined file modes that can be implemented when a file is opened. These modes define what can be done with the file once it is opened. If you do not mention the mode then “read” is considered the default mode. The list of various modes is given as follows:
‘r’ is also the default mode, it means that the file has been opened for reading purposes. You have already seen the usage of reading mode. To explain this a file by the name “test.txt” has been created. The content of the file is as follows:
“I am excited about writing on the file using Python for the first time. Hope You feel the same. ”
We will now give the following commands.:
>>> f_handle = open("test.txt" , 'r') >>> f_handle . read (4)
The output for the above code is :
f_handle.read(4) retrieves the first four characters from the file and displays it.
‘w’ stands for writing. It means that you want to open a file and write in it. In case the file that you have mentioned does not exist then a new file will be created.
>>> f_handle = open("test .txt",'w') >>> f_handle.write("I am excited about writing on the file using Python for the first time.") 71 >>> f_handle.write("Hope you feel the same.") 22 >>> f_handle.close( ) >>>
So, if you open the file now this is how the contents would look like:
The original content of the file before passing the write instructions was: “I am excited about writing on the file using Python for the first time. Hope you feel the same.” If you open the file after passing “write” instructions now the contents of the file will be as follows: “Hi I have opened this file again and I feel great again. ”
As you can see that the previous lines (/ am excited about writing on the file using Python for the first time. Hope you feel the same.) have been erased from the file.
Now, we close the file and try to write something again in it.
>>> f_handle = open (test. txt", ' w' ) >>> f_handle.write ("\n Hi I have opened this file again and I feel great again.") 58 >>> f_handle.close ( ) >>>
‘x’ stands for exclusive creation. An error will be displayed if the file already exists. Let’s see what happens when we try to use ‘x’ mode with an already existing test.txt file.
>>> f_handle = open("F:/test.txt"x') Traceback (most recent call last): File "<pyshell#l>", line 1, in <module> f_handle = open("F:/test.txt"x') FileExistsError: [Errno 17] File exists: 'F:/test.txt'
‘a’ is used to append an existing file. If a file does not exist then a new file will be created.
So, now we try to add new text to an already existing file.
The contest of test.txt file is as follows:
“I am excited about writing on the file using Python for the first time. Hope You feel the same. ”
We will try to add the following line to it:
“Hi I have opened this file again and I feel great again. ”
In order to append, we follow the following steps:
>>> f_handle = open("test.txt",'a') >>> f_handle.write("Hi I have opened this file again and I feel great again.") 56 >>> f_handle.close() >>>
I am excited about writing on the file using Python for the first time. Hope You feel the same. Hi, I have opened this file again and I feel great again.
‘t’ is used to open a file in text mode and ‘b’ is used to open the file in binary mode.
In the above examples, you must have noticed f_handle.close( ) command. It is important to use the close( ) command after you have stopped working with a file in order to free up operating system resources. If you leave the file open, you may encounter problems.
A better way of dealing with files is to keep the code related to file reading in a try block as shown in the following code:
>>> try: f_handle = open ("llllearning_files . txt") content = f_handle.read() f_handle.close() except for IOError: print (''Could not find the file. Please check again") exit( )
Could not find the file. Please check again
In the above piece of code, the file name given does not exist in the given location. Hence, the remaining code of the try block is ignored and the code written in the except block is applied. In the except block, we have provided a simpler and user-friendly message which is easier for the user to understand. Without the try expect to block the following message will be displayed:
Traceback (most recent call last): File "<pyshell#10>", line 1, in <module> f_handle = open ("llllearning_files .txt") FileNotFoundError: [Errno 2] No such file or directory: 'llllearning_files . txt'
Which can be difficult to decipher.
Now we will have a look at some very common file-system-type operations such as move, copy, and so on.
We now try to copy the contents of this file to another file. For this we require to import shutil as shown in the following code:
>>> import shutil >>> shutil. copy("F:/test.txt","F:/testl.txt") 'F:/testl.txt'
Content of testl.txt file:
You can move the file or change the name of the file using the move command as shown in the following code:
>>> import shutil >>> shutil.move("test.txt","test2.txt") 'test2.txt' >>>
The above set of instructions changes the name of the file test.txt to test2. txt.
Another important package available with Python is glob.
The glob package allows you to create a list of a particular type of files by using the star * operator.
>>> import glob >>> glob.glob("*.txt") ['important.txt', 'learning_files.txt', ' test1. txt', 'test2.txt'] >>>
How is a file opened for reading? Provide an example.
Using the open function, and the ‘r’ mode.
myFile = openCpathToFile’,’r’)
What is the appropriate method to call when a file is no longer being used?
What are the modes that a file can be opened in? Answer:
- ‘r’ – reading,
- ‘w’ – writing,
- ‘a’ – append,
- ‘r+’ reading and writing, contents intact,
- ‘w+’ reading and writing, contents deleted,
- ‘a+’ same as ‘r+’
What does the ‘b’ modifier do when appended to a file mode?
It changes the handling of the file from text to binary mode.
What does the ‘U’ modifier do when appended to a file mode?
It applies the universal new-line translator to the file when it is opened.
How is the buffer size specified in the file opening?
It is an optional parameter to the open function. A 0 indicates the file is unbuffered, 1 indicates that line by line buffering. Any other positive number is the actual buffer size to be allocated.
How is an entire file read into a buffer and returned as a string? When is it a bad practice to do so?
With the .read( ) method. When the file may be large, because the buffer may consume excessive memory.
How is a file read using a limited buffer size?
By specifying how many bytes to read in the read method. file.read(20) would read the next 20 bytes from the file.
How can a single line be read from a text file? Provide an illustration.
Using the linecache module.
myFile = “’file.txt”
print linecache.getline(myFile, 1) # prints the first line
print linecache.getline(myFile,40) # prints the 40th line, etc.
What are two ways to write a line of text to a file?
Using the .write method on an open file, or >>> redirection operator to use the print statement.
With a list of strings, how can they all be written out to a file in a single statement?
Using the .writelines( ) method. Example:
Illustrate how to determine the number of lines in a text file, using the readlines method. When would it be inappropriate to use this technique?
myLineCount = len(open(myFilePath, Y).readlines())
It would be inappropriate to use if the file might be large, because the readlines method will generate a list of strings that includes the entire file’s contents. It would consume too much memory.
What module and method is used to traverse the file system directory tree?
The os module. Specifically os.walk.
How are files deleted?
Using the os.remove method. os.remove(‘myFile’)
How are files renamed?
Using the os.rename method. os.renameColdFileName’, ‘newFileName’)
How are entire non-empty directory trees removed?
Each directory must be walked using the os.walk method, then the files within must be deleted. Then and only then can the directory be removed using the os.rmdir method.
How is a tarfile created?
Using the tarfile module.
my Tar = tarfile.open(“my Tarfile”, ‘w’)
How are files added to a tarfile or zipfile?
Using the .add method to an open tar or zip file.
How are files extracted from a tar or zip file?
Using the .extract method on an open tar or zip file, and specifying the file name and the extraction path.
Illustrate extracting all of the .txt files from a zip file.
myZip = zipfile.open(“myZip.zip “, Y)
for myFile in myZip.getnames( ):