Python File I/O

Updated: Aug 25, 2020

Python File I/O

Python uses file objects to interact with external files on your computer. These file objects can be any sort of file you have on your computer, whether it be an audio file, a text file, emails, Excel documents, etc.

File Modes in Python

Python Creating a file

With Python you can create files so lets create linuxadvise.txt by using the code

f= open("linuxadvise.txt","w+")

Now we will write text in linuxadvise.txt

f.write("Hello, this is a quick test file.")

This will close the instance of the file linuxadvise.txt stored


Python Opening a file

lets open linuxadvise.txt which we created just now

# Open the text.txt we made earlier
linux_advise = open('linuxadvise.txt')

# We can now read the file

Output :

Hello, this is a quick test file

# But what happens if we try to read it again?

Output :


This happens because you can imagine the reading "cursor" is at the end of the file after having read it. So there is nothing left to read. We can reset the "cursor" like this :

# Seek to the start of file (index 0)



 # Now read again


Hello, this is a quick test file.

You can read a file line by line using the readlines method. Use caution with large files, since everything will be held in memory. We will learn how to iterate over large files later in the course.

# Readlines returns a list of the lines in the file 


Hello, this is a quick test file.

When you have finished using a file, it is always good practice to close it.


Writing to a File

By default, the open() function will only allow us to read the file. We need to pass the argument 'w' to write over the file. For example:

# Add a second argument to the function, 'w' which stands for write. 
# Passing 'w+' lets us read and write to the file
linux_advise = open('linuxadvise.txt','w+')

Opening a file with 'w' or 'w+' truncates the original, meaning that anything that was in the original file is deleted!

# Write to the file
linux_advise.write('This is a new line')

Number of character in last line added into file

# Read the file


This is a new line

linux_advise.close() # always do this when you're done with a file

Appending to a File

Passing the argument 'a' opens the file and puts the pointer at the end, so anything written is appended. Like 'w+', 'a+' lets us read and write to a file. If the file does not exist, one will be created.

linux_advise = open('linuxadvise.txt','a+')
linux_advise.write('\nThis is text being appended to test.txt')
linux_advise.write('\nAnd another line here.')


This is a new line 
This is text being appended to test.txt
And another line here.


Iterating over file contents

Let's say you have a file with number of lines in it. How can you read each line of a file ?

Here is an example.

# Get the all the lines in file in a list 
listOfLines = list() 
with open ("linuxadvise.txt", "r") as myfile:
 for line in myfile:

Alright dear readers , you have come a really long way in Python. I hope this has been really beneficial for you. But this is just a start. Python is huge , really vast and trust me just go with the flow. Practice more and more , get your hands dirty, make mistakes, scratch your head, don't loose patience and finally shine like a star.

One day Python will be your best friend :-)

You have completed Python Beginner's course

24 views0 comments

Recent Posts

See All