Python has built in support for reading and writing files. The function readlines() can be used to read the entire files contents:
#!/usr/bin/env python filename = "readme.txt" with open(filename) as fn: content = fn.readlines() print(content)
Content will contain a list of all strings in the file.
To write files, you can use this code:
!/usr/bin/env python f = open("output.txt","w") f.write("Pythonprogramminglanugage.com, \n") f.write("Example program.") f.close()
On the first line we open the file for writing. The parameter “w” tells Python we want to open the file for writing. The write() calls write data into the file. Finally we close the file.