Python Read Text File and Python Write to Text file, CSV files etc

(Last Updated On: April 2, 2021)

Python read and write text files:

Before you can read or write a text file in Python, you must open the file with open. You pass the file name in the first parameter and in the second the desired access mode (see table1).

open delivers a file object that can now be edited with various methods (see table 2). file is one of the elementary Python data types. You need i.e. not importing a module to perform simple file operations.

Table 1: Access modes of the open method

mode Importance
‘r’ Read file (applies by default)
‘w’ Write file; existing files will be overwritten!
‘a’ write to the end of an existing file (append)
‘r+’ Read and write file

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


Table 2: Important methods for file objects

method Importance
s=f.read() Reads the entire file.
s=f.read(n) Reads n bytes and delivers them as a character string.
s=f.readline() Reads a line from the file.
f.write(s) Writes the string s to the file.
n=f.tell() Indicates the current read / write position.
f.seek(n, offset) Changes the read / write position.
close() Closes the file.

close() terminates access to the file and gives it back to other programs. You can do without close() in short scripts – at the end of the program all open files are closed in any case. Unless it’s the structure your code allows you to use all of the code to manipulate a file as in the Formulate the next example with open() as f. You won’t save yourself with that only the explicit close() call, you also make sure that the file is also closed when an error occurs.

With readline() you can read a file line by line. readline() always returns including the end of line character of each line, i.e. \ n on the Raspberry Pi. When the end of the file is reached, readline() returns an empty string. Because empty Lines within the file consist of at least \n, there are no ambiguities here. To process a text file line by line, you can also just use a for loop and save yourself the hassle with readline():

When writing text files don’t forget to include \n in the strings, you want to output. write() doesn’t care about line endings! Also note that, unlike print, write takes only one parameter and that this parameter really has to be a string. If you pay or other data in the text file, you must first save it in Convert strings.



The following program produces a text file with ten lines:

CSV files in Python

CSV files (Comma Separated Values) are text files with tabular data. The elements or columns are marked with a comma “,” depending on the origin of the data separated by a semicolon. Strings can also be split into double Quotation marks. Help with reading and writing such files the methods of the csv module.

With reader() or writer() you create a CSV read or a CSV write object;  pass the constructor a previously created file object and some parameters, which describe the syntax of your CSV file (see table 3).

Table 3: CSV format parameters

Parameter Importance
delimiter Specifies the column separator (default ‘,’).
lineterminator Specifies the line separator (s) (‘\ r \ n’).
quotechar Specifies how character strings are marked (‘”‘).
skipinitialspace Specifies whether blank lines should be skipped (False).
strict Indicates whether an error is triggered in the event of syntax errors (False).
escapechar Specifies which character the writer prefixes special characters.
doublequote Indicates whether apostrophes are doubled (True).

lineterminator only applies to writer objects. When reading, the reader comes automatically with any combination of ‘\r’ and \n. doublequote indicates how the CSV module should deal with the special case that a character string itself contains the quotechar character. By default, the character is then doubled doublequote = false, on the other hand, the character becomes the character to be set with escapechar in front, usually a \ -sign. Note that it is for escapechar there is no default setting.


To read a line with the CSV reader, simply form a for loop. The Loop variable contains the individual elements as a list:

With the CSV writer, the writerow method inserts a line in the CSV file. The elements must be transferred as a list:

Note that the CSV writer only has to put strings in quotation marks if otherwise a correct reading would not be possible. If every element should be enclosed in quotes, pass it to the constructor from writer the additional parameter quoting = csc.QUOTE_ALL.

Process measurement data

Let’s say you are monitoring a temperature with a 1-wire temperature sensor and save them regularly in a CSV file that has the following format:

2020 -01 -27 T10: 35, t = 23437

2020 -01 -27 T10: 40, t = 23500

From this file you want the average temperature, the minimum, for each day and extract the maximum. The result should be saved in two files: an easy-to-read text file and a CSV file. The text file should look like this:

2020 -01 -27 Minimum: 18.7 ° C Maximum: 24.3 ° C Average: 21.1 ° C

The CSV file, however, should use the standard formatting of the CSV writer:

2020 -01 -27, 18.7, 24.3, 21.1


The required Python code looks like this:

Here are a few notes: with open three files for reading and writing. In the Line date = … the first element is read from the list in line first. Must do it is a character string from which the first 10 characters are read will. This is the date.

temp = … acts similarly and extracts all characters except for the second column the first two (i.e. ‘t =’). The string is interpreted as a number with int and divided by 1000. If the CSV file does not comply with the prescribed format, a lot can go wrong – hence the error protection with try.


All temperatures for a day are saved in the temperatures list. If the date changes, so the condition date == date before is no longer met, the program determines the minimum, the maximum and the average value of temperatures. write writes the result to the text file Temperaturen.txt. The List temperatures is now re-initialized with the temperature of the new date. Note that the measurement data of the last day are not included in the result.

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

%d bloggers like this: