From the course: Using Python for Automation (2019)

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

How to write files

How to write files

- [Narrator] So I'm all set up on reading files, now it's time to check out how to write files. To demonstrate the flexibility of file writing, let's incorporate it into our existing code. Instead of printing the information to terminal, let's instead write it to a new file. Using the same open function we use to read files, let's now use it to write files. This line will create a new file, called PassFile.txt. Also notice how I'm using "w" as our second parameter, because we want to write to this file. Finally, let's change the print statement to a write function to update our newly created file. Remember to close the file when you're done. At this point, our code should produce a file called Passfile.txt that stores all the P's. With our code executed, let's go ahead an navigate to our current working directory. Great, our PassFile.txt now exists. Let's go ahead and open it and check if it only contains passes. Nice, our code is working as expected. Adding on…

Contents