Open Menu dzosoft
Close Menu dzosoft

   TOUT SUR L'INFORMATIQUE ET LA TECHNOLOGIE


                             




NICOTINE FREE

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)


También te puede interesar


Comment installer Python sur Windows

Comment écrire votre premier programme Python

Comment verrouiller un PC avec une commande vocale à l'aide de Python

Comment connecter la base de données MySQL à l'aide de Connector/Python

Comment dessiner des formes avec Python

Comment créer votre propre assistant personnel à l'aide de ChatGPT et Python

Comment créer et initialiser un dictionnaire Python


Leave comment
          

Enregistrez le pseudo et l'e-mail dans ce navigateur pour la prochaine fois.



Cargando...     

Hiding your face is not a solution!