How to Import File in Python

To import a file in Python, use the import statement. Importing a Python file is similar to importing a package. Python code is organized into both modules and packages.

First, we can create a Python file, and then we can use that Python file in any other file using the import statement.

In Python, the import keyword is used to make code in one module available in another. Therefore, imports in Python are essential for structuring your code effectively.

Let’s create a SUM.py file inside our current project folder. Then, write the following code inside the SUM.py file.

def sum(a, b):
  return a + b

Here, we created a SUM.py file that we can use in other Python program files.

We will create another app.py file and write the following code.

import SUM

num1 = int(input("Enter the value for a: "))
num2 = int(input("Enter the value for b: "))
sm = SUM.sum(num1, num2)
print(sm)

Output

Enter the value for a: 10
Enter the value for b: 10

20

In this program, we imported the SUM.py file using the import statement. In the next lines, we get two inputs for the variable num1 and num2. Next, we store the value of two inputs into num1 and num2. Then, we calculated the sum using a sum function inside the SUM.py file.

The sum() function has two arguments, a and b. We have passed the value for a and b as num1 and num2. The sum is found inside the sum function, and the sum value is returned. The returned value is stored in variable s. Then the sum is printed.

How to Import Module in Python

To import a module in Python, use the import statement and then write the module’s name. For example, Python has built-in modules like os, csv, and third-party libraries like numpy, pandas, scikit-learn, etc.

import csv

with open("sample.csv", "r") as file:
  data = csv.reader(file)
  for field in data:
     print(field)

In this program, we imported a built-in Python package called the csv. The csv package is mainly used for performing csv file operations. First, we opened the sample.csv file in reading mode. In the next line, we read the data from the csv file using a function called the csv.reader().

The csv.reader() function reads the content from the csv file. The content read from the sample.csv is stored in a data variable. This data variable can be used inside the file to work with the file data.

How to read a file in Python

To read a file in Python, use the file.read() function.

with open("output.txt", "r") as f:
 data = f.read()
print(data)

If the output.txt file has any content, it will print in the console.

In this example, we opened the output.txt file in the read mode. In the next line, we read all the file contents using the read() function. The f.read() function reads all the contents present in the file, and then this content is stored in a variable called the data. This data can be used to work with those file data.

That’s it for this tutorial.

See also

How to Convert File to String in Python

How to Save Array to File in Python

How to read binary files in Python

Leave a Comment