Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Organizing Your Screenshots

00:00 Another detail that’s not ideal of the take_screenshot() function right now is that you save the screenshot with the same filename. That means you are overriding the screenshot every time you are taking a screenshot.

00:12 In this lesson, you will first create a new folder where you save the screenshots in, and then you will create a function to check if a screenshot with a certain name already exists, and then increase the number so you don’t overwrite existing screenshots.

00:28 Start by creating a screenshots folder in the root directory of your project. As you can see on the left side, the screenshot folder is present.

00:36 There is currently a screenshot.png file from running the function before, so I don’t need that file anymore. I will delete this one.

00:47 So now we have a clear screenshot folder, no screenshots taken so far. In order to save the screenshot in the screenshots/ folder, we will use pathlib, and pathlib will also be part of creating unique filenames.

01:04 pathlib is part of Python’s standard library and we need the Path class to work with.

01:10 So from pathlib import Path,

01:16 and then create a function unique_path(). This function takes a folder name and a filename. So we can name the first parameter as directory and the second parameter as filename.

01:33 And then inside the function, you want to create a path variable, which is actually a pathlib object that combines the directory and the filename: path = and then you use the uppercase Path class and pass in the directory.

01:52 After the closing parentheses, write a forward slash and after that the filename

01:58 and then you return this path. This function will also make sure that the filename is unique, but for now we only want to have the path returned because then you can use it in a take_screenshot() function to get the file path.

02:12 Because currently in line 18, you are hardcoding the screenshot.png string and it would be way nicer to programmatically get this path from the unique_path() function.

02:24 So after you close the browser, in line 17, inside the take_screenshot() function, create two variables. One is the folder name and the other one is the filename. As foldername, it’s important that you use the screenshots folder without a slash and this must match exactly the folder that you just created. As the filename, you can continue to use screenshot.png.

02:52 And then you want to get the file path by calling the unique_path function

03:00 and passing in the foldername and the filename. So here I did something which is probably not super clean. I used here the variable foldername and filename and the parameters of my function are called directory and filename.

03:15 So probably it is a bit nicer to use the same variable names or completely different ones to make it more clear, but the code will still work. But yeah, I will change the foldername variable to directory as well.

03:33 And then instead of passing in screenshot.png to the open context manager, you can put the file path in directly. So once you save and you run python utils.py,

03:49 then you can see that the screenshot was successfully taken and saved in the screenshots folder. That’s great. That was the first step. So now when you run python utils.py again, you don’t want Playwright to overwrite the screenshot, but you want to create screenshot_01, for example.

04:09 So head back to the utils.py file and go into the unique_path() function. In order to not overwrite a screenshot over and over again, you want to create a prefixed filename.

04:22 So inside the unique_path() function, create a new variable named prefixed_filename, and this filename should look something like this.

04:32 If it’s the first file, we can call it 001, and then it should have an underscore. And then after that, you add the filename to it, which currently is screenshot.png.

04:46 And then when you create the path in the next line, you don’t want to use filename, but you want to use prefixed_filename.

04:55 So if 001 already exists, then this should be 002, 003, and so on. So this part here of the prefixed_filename should be dynamic.

05:08 And for that, you need to create a counter and a while loop and then format the counter into this prefix of the filename. The counter variable you can start with zero, and then you start a while loop while True, and now you have to indent the whole other stuff from the function, so the prefixed_filename, the path, and returning the path is inside of the while loop.

05:34 So at this point, the while loop would start, we would prefix the filename and then return the path immediately. So next, you want to have some kind of condition in it and only return if this path doesn’t exist yet.

05:47 So after creating the path, create an if condition, if not path.exists(), and only then return the path.

05:59 So at this point, however, we would create an infinite loop because if this file path exists because the file was written once, then we would just go on and go on and go on because you haven’t changed the prefix yet.

06:12 To do that, you first need to increase the counter so we don’t start with zero inside of the loop, but with one.

06:21 And then instead of writing inside the string of the prefix, so instead of the 003, you want to use a format specifier and then format in the counter.

06:32 So delete the 003 and add in curly braces, and inside the curly braces start with a colon and then 03d. So that means you want three digits and they should be zero padded.

06:47 This might make a bit more sense in a moment when we run the file. It’s important to note that we had 003 there, but we could have also had 004, 005, and so on. The three here now means that it’s three digits, and then after the string, you want to format in the counter.

07:08 And after you format in the counter, you continue to add the filename in the end. So now the filenames will be 001_screenshot .png, and if this file exists already, then it’ll be 002_screenshot .png, and so on.

07:27 Let’s try it out. Run python utils.py again,

07:33 and as you can see on the left side, you created 001_ screenshot.png. Run the file again, python utils.py,

07:43 and now you have 002_screenshot.png, and so on. That is very nice. Having the two zeros in front of the number implies that I’m planning on doing 999 screenshots, but I’m actually not planning on doing that many screenshots.

08:00 So let’s adjust the prefixed_filename part of it and only work with two digits. So now the string is curly braces, colon 02d, and then the closing curly brace and then underscore.

08:14 So now the prefix only has two digits, and now it starts with 01. Perfect. So before continuing, I will remove all the screenshots that we made in the screenshots directory so we continue clean in the next lesson.

Become a Member to join the conversation.