tutorialsPythonBasic.verybasic.files.file_writing module

An example on how to write to a file. Keep in mind that the paths for opening the file are relative, so you need to run this file from inside the folder it is located in for the tests to work.

tutorialsPythonBasic.verybasic.files.file_writing.writeToFile(filename, text)[source]

Write to the file named ‘filename’.

We will something and add the current date and time to the end so the file changes on each call.

>>> from datetime import datetime
>>> date = datetime.now()
>>> writeToFile('writing.txt', "Testing a file write. {0}".format(date))

It should be written now, let’s test it by opening the file and printing everything. We will ignore the date in the test since I can’t find a way to make it work properly right now :)

>>> with open('writing.txt') as f:
...     print(f.read()) 
Testing a file write. ...