Input-Output and Files in Python (Python Open, Read and Write to File)

Detailed Study of Input-Output and Files in Python: Python Open, Read and Write to File

Our previous tutorial explained about Python Functions in simple terms.

This tutorial we will see how to perform input and output operations from keyboard and external sources in simple terms.

In this Python Training Series, so far we have covered almost all the important Python concepts.

Watch the VIDEO Tutorials

Video #1: Input-Output and Files in Python

Video #2: Create & Delete a File in Python

Note: Skip at 11:37 minute in the below video to watch ‘Create & Delete a File'.

Input-Output in Python

Python provides some built-in functions to perform both input and output operations.

#1) Output Operation

In order to print the output, python provides us with a built-in function called print().

Example:

Print(“Hello Python”)

Output:

Hello Python

Python - output operation

Output:

Python - output operation - output

#2) Reading Input from the keyboard (Input Operation)

Python provides us with two inbuilt functions to read the input from the keyboard.

  • raw_input()
  • input()

raw_input(): This function reads only one line from the standard input and returns it as a String.

Note: This function is decommissioned in Python 3.

Example:

value = raw_input(“Please enter the value: ”);
print(“Input received from the user is: ”, value)

Output:

Please enter the value: Hello Python
Input received from the user is: Hello Python

input(): The input() function first takes the input from the user and then evaluates the expression, which means python automatically identifies whether we entered a string or a number or list.

But in Python 3 the raw_input() function was removed and renamed to input().

Example:

value = input(“Please enter the value: ”);
print(“Input received from the user is: ”, value)

Output:

Please enter the value: [10, 20, 30]
Input received from the user is: [10, 20, 30]

Python - input function

Output:

Python - input function - output

Files in Python

A file is a named location on the disk which is used to store the data permanently.

Here are some of the operations which you can perform on files:

  • open a file
  • read file
  • write file
  • close file

#1) Open a File

Python provides a built-in function called open() to open a file, and this function returns a file object called the handle and it is used to read or modify the file.

Syntax:

file_object = open(filename)

Example:

I have a file called test.txt in my disk and I want to open it. This can be achieved by:

#if the file is in the same directory
f = open(“test.txt”)
#if the file is in a different directory
f = open(“C:/users/Python/test.txt”)

Python - open file


We can even specify the mode while opening the file as if we want to read, write or append etc.

If you don’t specify any mode by default, then it will be in reading mode.

#2) Reading Data from the File

In order to read the file, first, we need to open the file in reading mode.

Example:

f = open(“test.txt”, ‘r’)

#To print the content of the whole file
print(f.read())

#To read only one line
print(f.readline())

Example: 1

Python - read data from the file

Output:

Python - read data file - output

Example: 2

Python - read single line

Output:

Python - read single line - output

#3) Writing Data to File

In order to write the data into a file, we need to open the file in write mode.

Example:

f = open(“test.txt”, ‘w’)
f.write(“Hello Python \n”)

#in the above code ‘\n’ is next line which means in the text file it will write Hello Python and point the cursor to the next line
f.write(“Hello World”)

Output:

Now if we open the test.txt file, we can see the content as:
Hello Python
Hello World

Python - write data to file

Output:

Python - write data to file - output

#4) Close a File

Every time when we open the file, as a good practice we need to ensure to close the file, In python, we can use close() function to close the file.

When we close the file, it will free up the resources that were tied with the file.

Example:

f = open(“test.txt”, ‘r’)

print (f.read())
f.close()

Python - close file

Output:

Python - close file - output

#5) Create & Delete a File

In python, we can create a new file using the open method.

Example:

f = open(“file.txt”, “w”)
f.close()

Python - create file

Output:

Python - create file-output

Similarly, we can delete a file using the remove function imported from the os.

Example:

import os
os.remove(“file.txt”)

Python - delete file

Output:

Python - delete file - output

In order to avoid the occurrence of an error first, we need to check if the file already exists and then remove the file.

Example:

 
import os
if os.path.exists(“file.txt”):
os.remove(“file.txt”)

print(“File deleted successfully”)
else:
print(“The file does not exist”) 

Using python input/output functions, we can get the input from the user during run-time or from external sources like text file etc. Hope you will be clear about Input-Output and Files in Python from this tutorial.

Our upcoming tutorial will explain about the various Types of Oops available in Python!!

PREV Tutorial | NEXT Tutorial