jed331
jed331

Reputation: 15

Escape Backslash with String Combination

I am working on a program that clicks a button when it changes color, and at the end of the program, it screenshots the webpage, saves the screenshot, and then moves it to a different directory. Everything seems to be working fine except for moving the file into a different folder. The code I am using to move the file is here:

os.replace("'\\'" + fileName, "'\\'" + saveName + "'\\'" + fileName)

I get the error:

FileNotFoundError: [WinError 3] The system cannot find the path specified: "'\'0.png" -> "'\'saves216'\'0.png"

I don't know how to get the backslash to escape without becoming a double backslash

Upvotes: 0

Views: 64

Answers (2)

cubesareneat
cubesareneat

Reputation: 322

os.replace("\\" + fileName, "\\" + saveName + "\\" + fileName)

extra '' in yours

change the filename variable too if it has ''

Upvotes: 0

Jarvis
Jarvis

Reputation: 8564

Remove the extra quotes:

os.replace("\\" + fileName, "\\" + saveName + "\\" + fileName)

You directly escape a \ with another one:

>>> s = "\\" + "filename"
>>> print(s)
\filename

Upvotes: 1

Related Questions