Change language

Working with files in Python


File handling &it is basically managing files on the filesystem. Each operating system has its own way of storing files.

Working with files in Python is useful to work with because we don’t have to worry about the underlying operating system, its rules, and file system operations.

file operations

1. open () function

The open () function is used to open a file in a specific mode.

Essentially, it creates a file object that can be used for further manipulation.

open (file_name, mode)
Various file open modes:

  • r: Read
  • w: write
  • a: add
  • r +: read and write

Initially, we need to create a file and place it in the same directory, as the script.

Welcome to the programming world!

 demo_file = open (’Demo.txt’,’ r’) # This statement will print every line in the file for x in demo_file: print (x) # close the file, very important demo_file .close ()  

Welcome to the programming world!
Here opens the Demo.txt file and prints all content line by line.

2. read () function

The read () function is used to read the contents of a file. To achieve the same, we need to open the file in read mode.

 demo_file = open ("Demo.txt", "r") print ( ()) demo_file.close () 

Welcome to the programming world!

3. write () function

The write () function is used to write to a file and make changes to it.

 demo_file = open (’Demo.txt’,’ w’) demo_file.write ("Hello Everyone !.") demo_file.write ("Engineering Discipline.") Demo_file.close () 

Output: when we open the file Demo.txt, we see the changes reflected here.
Hello Everyone !.
Engineering Discipline.

4. append ()

 demo_file = open (’Demo.txt’,’ a’) demo_file.write ("Statement added to the end of the file .. ") demo_file.close () 

Hello Everyone !.
Engineering Discipline.
Statement added to the end of the file. .

5. split ()

The split () function is used to split lines in a file. It is stripped as soon as it is encountered in a script.


 Hello Everyone !. Engineering Discipline. Statement added to the end of the file ..

 with open ("Demo.txt", "r") as demo_file: demo_d ata = demo_file.readlines () for line in demo_data: result = line.split () print (result) 


 [’ Hello’, ’Everyone! .’] [’ Engineering’, ’Discipline.’] [’ Statement’, ’added’,’ to’, ’the’,’ end’, ’of’,’ the’, ’file..’] 

6. close ()

The close () function is used to close manipulations with a specific file in it.

After writing to a file, unless we call the close ( ), all data written to the file will not be saved in it.

It is always recommended to close the file after we are done with it to free up resources.


7. Rename () function

The os module provides a rename () method to change the name of a specific file.

os.rename (current_name, new_name)

8. remove () method

The os module provides a remove () method to remove the file specified as input.

 import os os.remove (’Demo.txt’) 

Before executing the remove () method

delete file

Output: after executing the remove () method

 example after removing

Working with files in Python _files: Questions

Working with files in Python Python functions: Questions


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers



Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method