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.

Syntax:
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.

Demo.txt
Welcome to the programming world!  
Execute_file.py

 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 ()  

Exit:
Welcome to the programming world!
Here Execute_file.py 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.read ()) demo_file.close () 

Exit:
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 () 

Output:
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.



Demo.txt

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

Execute_file.py

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

Output:

 [’ 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.

Syntax:
file-name.close()

7. Rename () function

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

Syntax:
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

Shop

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

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () 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