From the course: Advanced Scripting for Testers: Local Data, Spreadsheets, and Reporting

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Interacting with files

Interacting with files

- [Instructor] Testers often need to inspect data in files and write reports or other data to files. Let's take a look at how you can manipulate files in Python. I'm working in Visual Studio Code, but you can use the code editor of your choice. We'll start with creating a simple text file. So to do that, we can use the open method. So with open, and let's create a file called example.txt. And we'll write to this file. Let's say "Hello, this is a test file." And then, I'm going to write a new line. So I'm doing going to do slash new line, and I'll write a second line. So we'll just write, this is a second line. Now you can see here that I'm using this w mode. So this is the name of the file to open and this is the mode that open is going to use. And that w means that we want to write to the file rather than say read from it. So I'll save this and let's try running it. So I can run the file by typing file_interactions. And let's check if that file is there. So we can see it's there and…

Contents