Here we are with the last article “Starting up with Python”. This one will shed some ideas on Working with files, Reading and Writing and Writing lines in Python. You can view all the past Python articles here.
- Seven steps to compile Python 3.5.0 from source
- Getting started with Python and Anjuta
- Starting up with Python – Part 1 – Numbers and Maths, Variables
- Starting up with Python – Part 2 – Modules and Functions, Strings
- Starting up with Python – Part 3 – Sequences and List, Slicing and Editing
- Starting up with Python – Part 4 – More list functions and Slicing lists
- Starting up with Python – Part 5 – Methods
- Starting up with Python – Part 6 – Sort and Tuples, Strings and stuffs, methods and Dictionary
- Starting up with Python – Part 7 – If statement, Else and elif statements
- Starting up with Python – Part 8 – Nesting and Comparison operators
- Starting up with Python – Part 9 – And and Or operators
- Starting up with Python – Part 10 – While, For, Infinite and Break Loops
- Starting up with Python – Part 11 – Building functions and Default parameters
- Starting up with Python – Part 12 – Multiple Parameters, Parameter Types and Tuples as Parameters
- Starting up with Python – Part 13 – Object Oriented Program and Classes and Self
- Starting up with Python – Part 14 – Subclasses, Overwrite variable on Sub, Multiple Parent classes
- Starting up with Python – Part 15 – Constructors, Import Modules, Reload Modules and Getting modules info
39. Working with Files
To write to a file is pretty simple. I have the file toto which is blank at /python/toto. After using the close function the phrase hey hackers mauritius is written to toto.
>> fileop=open('/python/toto','w')
>>> fileop.write('hey hackers mauritius')
>>> fileop.close()
To read from the file do the following. The number 3 here means 3 byte
>> fileop=open('/python/toto','r')
>>> fileop.read(3)
'hey'
If you want to read the whole file, just put nothing inside the read function. Its important to close the function to prevent memory leaks.
>> fileop.read()
' hackers mauritius
40. Reading and Writing
Let’s not read line by line. Here is how you read a whole line
>> fileop=open('/python/toto','r')
>>> print fileop.readline()
hey hackers mauritius
You can also read line by line and put it into a list
>> fileop=open('/python/toto','r')
>>> print fileop.readlines()
['hey hackers mauritius\n', 'how are you doing\n', 'what are the new projects?\n']
To create a new file or overwrite a file with some lines do this
>> fileop=open('/python/toto','w')
>>> fileop.write('This is a new LINE\n')
>>> fileop.close()
41. Writing Lines
Let’s now create a list and store it into a temporary variable called listtest. listtest is now a list of all lines in toto.
>> fileop=open('/python/toto','r')
>>> listtest=fileop.readlines()
>>> listtest
['This is a new LINE\n']
>>> fileop.close()
We can also modify the same list. Here [1] is the second line as it starts with 0
>> filelist=open('/python/toto','r')
>>> listtest=filelist.readlines()
>>> listtest
['This is a new LINE\n', 'this is another line\n']
>>> filelist.close()
>>> listtest[1]="this is second line"
>>> listtest
['This is a new LINE\n', 'this is second line']
However, this has not been saved to the file. Let’s see how to save it.
>> filelist=open('/python/toto','w')
>>> filelist.writelines(listtest)
>>> filelist.close()