File handling
Managing files is one of the most important features of any programming language. Python supports file handling via the io module's TextIOWrapper class, docs
There are two basic modes in which we can manipulate files, Text and Binary.
Writing files
Write all even numbers till 100 to a file named "even.txt" and write all odd numbers to "odd.txt".
Why close a file?
When we write anything to a file, it gets written to a buffer and not directly to the underlying file. flush()
method can be used to immediately write to the file. The close()
method internally calls the flush()
method, thus, in most of the cases, it is enough to just call the close()
method.
Reading files
Read all the content from the file "even.txt" and print it to the terminal
You will see that each number is printed on a different line. This is because of the new line ("\n") character which we wrote.
The case of the new line.
Whenever we read or write to a text file, we are responsible for the new line character "\n". When a file's content is "python\nis a good language.", it is displayed as
on any text editor, because the "\n" gets converted into an actual new line. But, the content of the text file is still "python\nis a good language."
Output: ['01234']
We can see that the file contains only one line. This is because, the write
method doesn't add a new line character at the end of each operation. To understand it better, open this file in a text editor.
Now, consider this block.
Output:
['0\n', '1\n', '2\n', '3\n', '4\n']
By adding a "\n" in each write call, we ensure that the new number gets written to a new line, that's why, when we run the above code, we will get four lines in the text file. Verify this by opening the file in a text editor.
While reading a file, we need to deal with the "\n" present at the end of each line. There is a shortcut to remove the "\n".
List comprehension
It is a shortcut of working with lists, to perform filter or some other operation on the entire list.
List comprehension replaces the following block:
Syntax
List comprehension returns a new list based on the current list, the first argument list.strip()
would be the elements of the list. The second argument is the for loop, the third argument is optional, you can have an if
block there.
Examples:
Convert each element to upper case (works only when each element is a string).
Convert each element to lower case (works only when each element is a string).
Give all the values of list a which are greater than 3.
Give all the values of list a which are greater than 3.
Give all values in a which are even.
Appending files
Write even numbers from 100 to 200 in a file "even.txt" which already contains even numbers from 0 to 100.
Binary Mode
Python allows us to manipulate binary files, we have to club the modes, b
stands for binary, along with b
, we can use any of r/w/a.
Read a binary file: "rb".
Write a binary file: "wb".
Note: One essential part of working with files is removing the
\n
or adding it when it is required.
Other methods
read()
Takes an argument as number of bytes to be read.When you open a file for reading, the pointer is at the 0'th position. if you do
f.read(1)
, the pointer moves to 1. If you dof.read(1)
again, the pointer would return the 2nd character of the file and not the first one.All the read functions returns characters based from the current pointer, you can know where the pointer is located currently by using
f.tell()
.Note: This is a session for the Interpreter.
In this example, you hit the end of the file, there are only 38 characters in it, "\n" also is a character. If you read beyond the max characters of a line, it will return you blank strings. but there might be cases in which you would want to start reading the file again from any other position.
You can use
seek
function to reset the pointer at any position of your choice. Let's say that you want to re-read the file againThis will reset the pointer and you should be able to read it from the first character of the file!
readline()
Returns the current line that the pointer is located.You can try doing
f.seek(0)
and reading the lines again, you'll get the same output as above.readlines()
Returns all the lines of the file as a list.If the pointer is present at the end of file, then it returns an empty list. In that case, do a
f.seek(0)
to reset the pointer.write()
Writes the string argument which you pass on the pointer. We need to be careful of the pointer, otherwise writing to the file can be dangerous. You are responsible for adding the "\n" character if you want to a new line, by default it'll write at the position the pointer is currentlf.tell()
.writelines()
If you want to write a list to a file, you'll use writelines. Even here you are responsible for adding \n wherever you feel necessary.We encourage you to try out file IO on your own, please refer to help() for any details regarding file, do not search "how to open a file in Python" on the internet.
Exercise
Take the user's name, age and height as input and write them to a file using this format. name,age,height.
Read a file "input.txt" and print all the lines which are present multiple times. Please create the input.txt file in such a way that it has many lines and few of them are present multiple times.
Read a file "input.txt" and print how many times each line is present.
There are two files, "file1.txt" and "file2.txt"
swap the content of both files.
append the content of file1.txt to file2.txt.
append content of file2.txt to file1.txt.
take unique content of both files and write them to file3.txt
Use any file created above. Take a positive number as input from the user and read those many characters from the text file.
Prepare a csv file like this: first field = name, second onwards marks/100.
Print the name of the student with highest marks.
Print the name of the student with highest marks in upper case.
Print the highest score.
calculate the total score of each student, add the total at the end of the line and write everything to result.csv
sample output:
Read the result.csv file and create a new file named result.txt like the following sample output.
read the result.txt file. Print every other character from the file.
Links
Last updated