Tag Archives: readline

Reading and Writing to a File in Python

Reading and Writing to a File in Python

After having opened our file in the “r”, “w”, or “a” mode, we can read and write to it.

>>> var = open(“D:/derek.txt”, mode = “r”)
>>>

You see a file as an icon in your operating system. It is divided into some parts and saved on various places on hard disks. But for the read() and write() functions in Python, our file is just a recorded string of characters. Python reads the file character by character and line by line. It starts from the first character and finishes at the end one. It can’t go back, skip something — it has to simply read. It is useful to save all in a variable, for example:

var1 = our_file.read()

When we do that, only one line of our file will be in var2 with EOL (end-of-line) character that is LF (UNIX/Linux), CRLF in (Windows/DOS), or CR (MacOS). And we are at the beginning of the next line. readline() is perfect for loops and line lists.

>>> var1 = var.read()
>>> var1
“Ala is 23 years old.\nJola is 170 cm high.\nOla is here.\nAnna is a Ola’s si
ster.\nHanna is Maliszewski’s daughter.\nMaria is a well-educate woman.\nJolanta
 died one year ago.”
>>>

When we do that, only one line of our file will be in var2 with EOL (end-of-line) character that is LF (UNIX/Linux), CRLF in (Windows/DOS), or CR (MacOS). And we are at the beginning of the next line. readline() is perfect for loops and line lists.

var2 = our_file.readline()

When we do that, only one line of our file will be in var2 with EOL (end-of-line) character that is LF (UNIX/Linux), CRLF in (Windows/DOS), or CR (MacOS). And we are at the beginning of the next line. readline() is perfect for loops and line lists.

>>> var2 = var.readline()
>>> var2
‘Ala is 23 years old.\n’
>>> var2
‘Ala is 23 years old.\n’
>>>

It is the same line in the var2. And we want our var2 include next lines, so we have to call readline() again, and then again:

>>> var2 = var.readline()
>>> var2
‘Jola is 170 cm high.\n’
>>> var2 = var.readline()
>>> var2
‘Ola is here.\n’
>>> var2 = var.readline()
>>> var2
“Anna is a Ola’s sister.\n”
>>> var2 = var.readline()
>>> var2
“Hanna is Maliszewski’s daughter.\n”
>>> var2 = var.readline()
>>> var2
‘Maria is a well-educate woman.\n’
>>> var2 = var.readline()
>>> var2
‘Jolanta died one year ago.’
>>> var2 = var.readline()
>>> var2
‘ ‘
>>>

we’ve got to the empty line — the reading was finished.

var3 = []

var3 = our_file.readlines()

When we do that, all content of our file, line by line, will be in var3. The lines will be separated as items in this array.

When we are the end of our file and when we try to read further, we’ll get an empty string that is ‘ ‘. Reading the empty line, we’ll get \n.

>>> var3 = []
>>> var3 = var.readlines()
>>> var3
[‘Ala is 23 years old.\n’, ‘Jola is 170 cm high.\n’, ‘Ola is here.\n’, “Anna
is a Ola’s sister.\n”, “Hanna is Maliszewski’s daughter.\n”, ‘Maria is a well-ed
ucate woman.\n’, ‘Jolanta died one year ago.’]
>>>

Reading the file, we’ll get string datatype only that is str. If wish to have other datatypes, we need to converse it. When one line includes one integer, we’ll do that:

var4 = int(our_file.readline())

>>> var4 = int(var.readline())
>>> var4
2
>>>

>>> type(var4)
<class ‘int’>
>>>

>>> var4 = float(var.readline())
>>> var4
2.0
>>> var4 = float(var.readline())
>>> var4
0.5
>>>

>>> type(var4)
<class ‘float’>
>>>

If our line has more items we are interested in, we need to extract them from the text. In the beginning, we have to read one line we interested in:

var5 = our_file.readline()

Then, we use split() function. It divide our text into items using a separator that we have in parentheses. You see that they are empty but it means that the default separator will be used that is a space.

var6 = var5.split()

Now, we can converse the split text (the items of our array) into the datatype we wish.

var7 = [int(var6[0]), float(var6[1]), int(var6[2])]

Sometimes, it better to use [:] to slice our text.

>>> var5 = var.readline()
>>> var5
‘1\n’
>>> var6 = var5.split()
>>> var6
[‘1’]
>>> var7 = [int(var6[0])]
>>> var7
[1]
>>> var7 = [float(var6[0])]
>>> var7
[1.0]
>>>

To read our file, we can use the for loop going through readlines(). The lines are read in turn, and we have a nice output thanks to the print() function.

var8 = open(our_file, mode = “r”)

for line in var8.readlines():

print(line)

var8.close()

>>> var8 = open(“D:/derek.txt”, mode = “r”)
>>> for line in var8.readlines():
…     print(line)

Ala is 23 years old.

Jola is 170 cm high.

Ola is here.

Anna is a Ola’s sister.

Hanna is Maliszewski’s daughter.

Maria is a well-educate woman.

Jolanta died one year ago.
>>> var8.close()
>>>

We can write to our file in this way:

our_file.write(“string”)

When we do that, we’ll write the string to our file at the end. It can write only string, so when you wish to write other datatypes, you are  forced to converse them to str using str(). write(0 doesn’t write to the file nothing but our text.

>>> var = open(“D:/derek.txt”, mode = “a”)
>>> var.write(“Aga like you all”)
16
>>>

our_file.writelines(array)

When we do that, we’ll write the string list to our file taken from the array, not dividing them with any separator. To make our strings placed in separated lines, we should end them with \n.

>>> ol = []
>>> ol = [Ewa, Mika, Renia]
>>> ol = [“Ewa”, “Mika”, “Renia”]
>>> ol
[‘Ewa’, ‘Mika’, ‘Renia’]
>>> var.writelines(ol)
>>>

>>> var.close()
>>> var = open(“D:/derek.txt”, mode = “r”)
>>> var.readlines()
[‘Ala is 23 years old.\n’, ‘Jola is 170 cm high.\n’, ‘Ola is here.\n’, “Anna
is a Ola’s sister.\n”, “Hanna is Maliszewski’s daughter.\n”, ‘Maria is a well-ed
ucate woman.\n’, ‘Jolanta died one year ago.Aga like you allEwaMikaRenia’]
>>>

We can write to the file only strings. Other datatypes should be converted first, for example:

our_file.write(str(integer))

>>> var = open(“D:/dereka.txt”, mode = “a”)
>>> f = 200
>>> type(f)
<class ‘int’>
>>> var.write(str(200))
3
>>>

>>> var.close()
>>> var = open(“D:/dereka.txt”, mode = “r”)
>>> var.readlines()
[‘\n’, ‘1\n’, ‘2\n’, ‘0.5200’]
>>>

Great!