pythontestcomplete

Python Script Test complete remove space in file save name


Hi i am currently trying to save a picture to a file however it is currently saving as C:/screenshots/ 1.png with a space between the / and the 1 i want to get rid of that

The 'SaveToFile' function is a test complete specific but im guessing my problem is my python syntax

The line that saves it does this

pic.SaveToFile("C:\screenshots\ "+ str(counter) + ".png")

So i thought the obvious solution was to do this

pic.SaveToFile("C:\screenshots\"+ str(counter) + ".png")

But that produces an error

I then thought i could double quote it

pic.SaveToFile("C:\screenshots\""+ str(counter) + ".png")

But the picture doesnt save

How do i get it to save the picture as

C:/screenshots/1.png

Any help would be appreciated thanks


Solution

  • Put backslash before backslash to escape it

    pic.SaveToFile("C:\\screenshots\\"+ str(counter) + ".png")
    

    As-is, in

    pic.SaveToFile("C:\screenshots\"+ str(counter) + ".png")
    

    The \" is interpreted as escaped-quote, so that the string does not end there, and quotes are unbalanced.

    You can avoid the double-backslash with r for raw-string.

    pic.SaveToFile(r"C:\screenshots\"+ str(counter) + ".png")
    

    You could also write

    pic.SaveToFile("C:/screenshots/"+ str(counter) + ".png")
    

    and it would work.

    You also don't need str(counter) if you write formatted-string with f.

    pic.SaveToFile(f"C:\\screenshots\\{counter}.png")