Open Menu dzosoft
Close Menu dzosoft

   ALL ABOUT COMPUTER SCIENCE AND TECHNOLOGY


                             




Hiding your face is not a solution!

Publish perfectly-optimized content in 1-click









 
 
 

How to create read and write to a file in python

 
 
How to create read and write to a file in python
 

One way to store data permanently is to store it in files.

 

Edit a file

 
To edit a file in python we use the function open .

This function takes as first parameter the path of the file (relative or absolute) and as second parameter the type of opening

 

Relative path / absolute path

 
A relative path in computer science is a path that takes the read location into account.

An absolute path is a full path that can be read from anywhere.

 

The open function

Here is the syntax to read a file:


>>> file = open ( "data.txt","r" ) >>> print file < open file 'data.txt', mode 'r' at 0x7ff6cf3fe4b0 >


Note that the second parameter is filled in by a r , this parameter indicates a file opening for reading .

 

Types of opening

There are several opening modes:

r , for an opening in reading (READ).
w , for a write opening (WRITE), the file content is overwritten each time it is opened. If the file does not exist python creates it.
a , to open in APPEND mode. If the file does not exist python creates it.
b , for opening in binary mode.
t , to open in text mode.
x , create a new file and open it for writing

 

Closing a file


Like any open element, it must be closed once the instructions have been completed. For this we use the method close() .
>>>  file . close ()


 

Read the contents of a file


To display all the contents of a file, you can use the method read on the file-object.


# coding: utf-8 

file = open("data.txt" , "r" ) print file.read () file.close ()


 

Write to file


Here is the syntax for writing to a file:

file  =  open("data.txt", "a" ) 
file.write( "Hello World" ) 
file.close()


Note that for the opening world a , if you want to write on the line, you can use the line break \n :

file  =  open ("data.txt","a" ) 
file.write("\n Hello World" ) 
file.close()


 

The keyword with


There is another shorter syntax which allows to emancipate oneself from the problem of closing the file: the keyword with .

Here is the syntax:

with  open( "data.txt","r" ) as file : 
	print file.read ()


 

Nmap

 

"mmap" is a Python library that allows mapping files or memory segments in Python. It provides an interface that resembles Python arrays, allowing file or heap data to be accessed as if it were a Python array.

Using "mmap" can be a more efficient approach to accessing data from a file or memory segment than using standard read and write functions, as it maps the file or the memory segment in memory and to access the data directly, without going through the overhead of the Python interpreter. This can be particularly useful for processing large amounts of data or for accessing data repeatedly.

Here's how to use "mmap" to read a file in Python:


import mmap
# Open the file in read mode and get its size with open('my_file.txt', 'r') as f: size = f.seek(0, 2) f.seek(0)
# Create an mmap object from file mm = mmap.mmap(f.fileno(), size)
# Read contents of file using mmap object contents = mm.read()
# Print contents of file print(contents)


It might also interest you


How to install Python on Windows

How to write your first Python program

How to lock PC with voice command using Python

How to connect MySQL database using Connector/Python

How to draw shapes with python

How to create your own personal assistant using ChatGPT and Python

How to create and initialize a Python dictionary


Leave comment
          

Save nickname and email in this browser for the next time.



Loading...     
close publicity
Nord VPN