Lesson 14

This is python lesson 14

In this lesson we will be moving on again. We will be moving on to files. Python can access files in your computer and change them. We will be using this to make a simple calendar later on.

f = open(“Insert a file directory”, “r+”)

f.truncate()

f.write(“PythonStar is amazing.”)

for line in f:

with open(“Insert your file directory again”, “r+”) as f:

print(f.read())

The above piece of code does many things. First Python opens your file in the background. Then f.truncate() deletes the content. f.write adds text to the file. You can also write a variable. The next few lines open the file for reading. r+ means for reading and writing. Then it prints the contents. You can continuously write an ever changing variable to a file or something. Check out the calendar on example codes for a useful use for files in Python.