Modifications

Sauter à la navigation Sauter à la recherche
625 octets ajoutés ,  8 mars 2022 à 01:16
Ligne 35 : Ligne 35 :     
=== Reading data from text file ===
 
=== Reading data from text file ===
{{traduction}}
+
The following script read the content of the "file2.txt" text file and displays it on the REPL output. 
 +
 
 +
<syntaxhighlight lang="python">
 +
with open( 'file2.txt', 'r' ) as _file:
 +
    s = _file.readline()
 +
    while s:
 +
        print( s.strip('\r\n') )
 +
        s = _file.readline()
 +
</syntaxhighlight>
 +
 
 +
Notes:
 +
* the ''''r'''' mode is used to read text file.
 +
* the {{fname|readline()}} read and return a line from the file (including the carriage return & line feed.
 +
* the {{fname|strip('\r\n')}} remove the carriage return & line feed from the readed line. Otherwise it will also be printed to the REPL (which result into a double line feed).
    
== File-system, Writing and data lost ==
 
== File-system, Writing and data lost ==
29 917

modifications

Menu de navigation