Python File I/O

Python File I/O

Learn to perform various file operations in Python

Introduction to Python I/O

I/O stands for Input/Output.

There are some commonly used built-in functions in Python like input() and print(), that help with input and output operations respectively.

The input() function reads user input into memory which is defined as sys.stdin and the print() function send data to display and is identified as sys.stdout.


Input operation with input()

To read input from user, you can use the input() built-in method.

**All codes are tested with Python 3.8.2.

Example 1

user_name = input('Enter your name--> ')  
print(f"User name is {user_name}")

Output

Enter your name--> Pylenin
User name is Pylenin

The input() function reads a line from input, converts it to a string (stripping a trailing newline), and returns the string.

Example 2

value = input("Enter anything==> ")
print(type(value))
print("Input received from the user is: ", value)

Output

Enter anything==> [1, 2]
<class 'str'>
Input received from the user is:  [1, 2]

To learn more about the input() function, check out this article written by Pylenin.


Output operation with print()

The print() function provides an interface to the standard output(sys.stdout) object. When you use print, you are asking your Python interpreter to echo your message to standard output.

Example 1

print('Python', 3, 'Rocks', sep='|')

Output

Python|3|Rocks

Check out this deep-dive article on print() to learn more about it.


How to open and read a file in Python?

When you want to read from or write to a file, you need to open it first. Once you have performed the necessary operations, it needs to be closed so that the changes are saved properly.

Hence, in Python, a file operation takes place in the following order:

  1. Open a file
  2. Perform your operation
  3. Close the file

To open a file in Python, use Python's inbuilt function open() and specify the mode, which represents the purpose for opening the file.

Below is an example of a program that opens a csv file in reading mode

Example 1

for data in open('test.csv', 'r', encoding='utf-8'):
  print(data)

Output

name, age
Pylenin, 2
Hello, 4

Note - The reading mode is also the default mode. So if you don't specify any mode, Python treats it as a reading mode.

Below is the table showing the other useful modes.

ModeDescription
rOpens a file in reading mode.
wOpens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.
aOpens the file in append mode. The file pointer exists at the end of the previously written file if exists any. It creates a new file if no file exists with the same name.
bOpens a file in binary mode.
r+Opens a file for both reading and writing.


Do you need to use encoding with open() method?

Normally, files are opened in text mode, that means, you read and write strings from and to the file, which are encoded in a specific encoding. Because UTF-8 is the modern de-facto standard, encoding='utf-8' is recommended unless you know that you need to use a different encoding.

Appending a b to the mode opens the file in binary mode. Binary mode data is read and written as bytes objects. You can not specify encoding when opening file in binary mode in Python.

Example 2

for data in open('test.csv','rb'): #binary mode
  print(data)

Output

b'name, age\n'
b'Pylenin, 2\n'
b'Hello, 4'

How to write to a file in Python?

To only write(not append) into a file in Python, you have to open the file in write w mode.

Be careful with the w mode, as it overwrites the file if it already exists and all the previous data is erased.

If the file doesn't exist, a new file is created.

Example 1

with open("my_essay.txt",'w',encoding = 'utf-8') as f:
   f.write("My name is Pylenin.")
   f.write("I am a Modern Data Architect.")
   f.write("Follow me on Twitter @pylenin.")

The above code will create a file called my_essay.txt with the lines written on the same line.

my_essay.txt

My name is Pylenin.I am a Modern Data Architect.Follow me on Twitter @pylenin.

To write each line to a new line, use a \n at the end of each line.

Example 2

with open("my_essay.txt",'w',encoding = 'utf-8') as f:
   f.write("My name is Pylenin.\n")
   f.write("I am a Modern Data Architect.\n")
   f.write("Follow me on Twitter @pylenin.\n")

The above code will create a file called my_essay.txt with the lines written on the same line.

my_essay.txt

My name is Pylenin.
I am a Modern Data Architect.
Follow me on Twitter @pylenin.

How to append to a file in Python?

To add more data to an existing file without overwriting, use the append a mode. If the file doesn't exist, it creates a new file.

Example 1

with open("my_essay.txt",'a',encoding = 'utf-8') as f:
   f.write("My new website is www.100daysofdata.com.\n")

The above code will create a new line in my_essay.txt file.

my_essay.txt

My name is Pylenin.
I am a Modern Data Architect.
Follow me on Twitter @pylenin.
My new website is www.100daysofdata.com.

How to close a file in Python?

When you are done with performing operations on the file, you need to properly close the file. To close a file in Python, use the close() method.

Even if you don't close the file explicitly, Python has a garbage collector which cleans up unreferenced objects.

However, this is not good practice! Closing a file, frees up the resources that are tied with the file.

Example 1

try:
   f = open("my_essay.txt",'a',encoding = 'utf-8')
   # perform file operations
finally:
   f.close()

Notice the use of try, except and finally blocks to handle opening and closing the files. This allows you to handle any exception that might arise while working with the file.

To learn more about handling exceptions in Python, check out these list of articles.

with statement in Python

If you are using the with statement while opening files, you don't need to use the close() method.

with statement in Python is used in exception handling. By using the with statement, Python ensures that file is automatically closed and resources released, once the statement is done executing.

Example 2

 with open("my_essay.txt",'a',encoding = 'utf-8') as f:
     # perform file operations

How to read and write to a file at the same time?

In order to perform simultaneous read/write operations, use the r+ mode.

Example

with open("my_essay.txt",'r+',encoding = 'utf-8') as f:
   lines = f.read()
   print(lines)
   f.write("Hello Data community!\n")


with open("my_essay.txt",'r+',encoding = 'utf-8') as new_data:
  print(new_data.read())

In the above example, we are using the r+ mode to read from my_essays.txt file and simultaneously writing a new line to it.

Output

My name is Pylenin.
I am a Modern Data Architect.
Follow me on Twitter @pylenin.
My new website is www.100daysofdata.com.

My name is Pylenin.
I am a Modern Data Architect.
Follow me on Twitter @pylenin.
My new website is www.100daysofdata.com.
Hello Data community!

Python File Methods (with examples)


readline()

This method reads the current line till it encounters a newline character.

Example

with open("my_essay.txt",'r',encoding = 'utf-8') as f:
   lines = f.readline()
   print(lines)

Output

My name is Pylenin.


readlines()

The method reads all the lines and returns a list object.

Example

with open("my_essay.txt",'r',encoding = 'utf-8') as f:
   lines = f.readlines()
   print(lines)

Output

['My name is Pylenin.\n', 'I am a Modern Data Architect.\n', 'Follow me on Twitter @pylenin.\n', 'My new website is www.100daysofdata.com.\n']


writelines()

The method writes a list of items to a file.

Example

lines = ['My name is Pylenin.\n', 'I am a Modern Data Architect.\n', 'Follow me on Twitter @pylenin.\n', 'My new website is www.100daysofdata.com.\n']

with open("my_essay.txt",'w',encoding = 'utf-8') as f:
   f.writelines(lines)

my_essay.txt

My name is Pylenin.
I am a Modern Data Architect.
Follow me on Twitter @pylenin.
My new website is www.100daysofdata.com.


seek() and read()

These are two interesting methods in Python. The seek(position) method brings the cursor to the specified position. The read(size) method reads from the cursor position till the specified size. If no arguments are passed, it reads till the end of file.

Example

with open("my_essay.txt",'r',encoding = 'utf-8') as f:
   f.seek(10)
   data = f.read()
   print(data)

The above code, moves the cursor to the 10th position. From there when you call the read() method, you are only able to see partial data.

Output

 Pylenin.
I am a Modern Data Architect.
Follow me on Twitter @pylenin.
My new website is www.100daysofdata.com.

For any doubts and queries, use the comments section or tweet to me @pylenin.

Did you find this article valuable?

Support 100 days of Data by becoming a sponsor. Any amount is appreciated!