Beruflich Dokumente
Kultur Dokumente
That object provides basic functions and methods necessary to manipulate files
by default.
Before you can read, append or write to a file, you will first have to it using
Python's built-in open() function.
In this post I will describe how to use the different methods of the file object.
Open()
The open() function is used to open files in our system, the filename is the
name of the file to be opened.
The mode indicates, how the file is going to be opened "r" for reading,
"w" for writing and "a" for a appending.
The open function takes two arguments, the name of the file and and the mode
for which we would like to open the file.
By default, when only the filename is passed, the open function opens the file
in read mode.
Example
This small script, will open the (hello.txt) and print the content.
This will store the file information in the file object "filename".
filename = "hello.txt"
file = open(filename, "r")
for line in file:
print line,
Read ()
The read functions contains different methods, read(),readline() and readlines()
To append to an existing file, simply open the file in append mode ("a"):
Close()
When you�re done with a file, use close() to close it and free up any system
resources taken up by the open file
File Handling Examples
Let's show some examples
To open a text file, use:
fh = open("hello.txt", "r")