When you close a program, all the data are disappeared and if you run it again, then it starts with a clean slate. So when you want to store your program data to manipulate them at later date, you simply need to preserve it by writing files.
First of all you need to give Python access to a file by opening it. You can do it by using open() function. The open function takes two parameters-first name of the file to be accessed, and second a mode. The available modes are-
1.’r’:Read mode(file is opened for reading)
2.’w’:Write mode(file is opened for writing and if a file with the same name is exists, then it will be erased)
3.’a’:Append mode(file is open for appending, which means data is to be added with the existing data)
Lets write our first file-
#opens file in writing mode which creates a file named “welcome.txt”
>>> f=open("welcome.txt","w")
>>> print f
<open file 'welcome.txt', mode 'w' at 0x00AFBC80>
Methods of file object
With the statement “f=open(“welcome.txt”,”w”)”, an object of the class ‘file’ is created. To read from or write to the file read, readline, or write methods are used. Finally, when works with files are finished, then close method is used to close the file.
write method to file object is used write into files. Here is how to do this-
#opens file in writing mode f=open(“welcome.txt”,”w”) content=’’’Hi! Welcome to Python. Hope you enjoy it.’’’ #write content to file f.write(content) #close the file f.close()
To read the contents of a file, read method is used. Take a look at the following code-
#opens the file in reading mode
f=open("welcome.txt","r")
#here argument size is used to read mehtod
f.read(12)
#here no argument is supplied
f.read()
#close the file
f.close()
The code above will result the following output-
'Hi!\nWelcome '
'Hi!\nWelcome to Python.\nHope you enjoy it.'
Note: f.read(size) consists an optional argument size. If it is negative or not supplied, the entire content will be read. Or otherwise you can specify it.
We can also read the contents of a file line by line by using looping statement-
f=open("welcome.txt","r")
for line in f:
print line
Which results the following output-
Hi!
Welcome to Python.
Hope you enjoy it.
Another method to file object is used to read contents of a file named ‘readline’. See how it works-
f=open("welcome.txt","r")
f.readline()
f.readline()
f.readline()
f.readlines()
The output is-
'Hi!\n'
‘Welcome to Python.\n’
‘Hope you enjoy it.\n’
['Hi!\n', 'Welcome to Python.\n', 'Hope you enjoy it.']
f.readline() just read a single line. When you use f.readline() for the first time in a program, it will read first line. Next time use of f.readline() results the read of second line and so on. If you wish to read the entire file, then use f.readlines(). It is a good practice to close a file after your works are finished.
These are the basic methods to the file object. There are other methods also. You may take a look at these by entering the command help(file) to your interpreter. Hope you clearly understand this tutorial.
Happy coding!





MultiQuote






|