Python-Ref > Using files > Reading and writing files > Reading a file
 
 

^^->
Klíčová slova
Moduly
Knihovní funkce

Reading a file

How to read the content of a file all at once.
To open a file and read its content into one big string is a simple procedure.
Expand/Shrink
This is an example text in an example file.
It even has
several
lines.
Zdroj: (fileio1.py)
  1   f = file( "example.txt", "r")  # we open the file "example.txt" for "r"eading
  2   content = f.read()             # we put the whole content of this file to a variable 'content'
  3   print content                  # print the content
  4   f.close()                      # we close the file
stdout:
This is an example text in an example file.
It even has
several
lines.

Doba běhu: 21.6 ms
Do not forget to close the file when you are done with it.