-1

I'm writing a function that receives string of path and in the example I have, it has the "\v" in it , how do I solve it ?

getting error : OSError: [Errno 22] Invalid argument: 'C:\x0baction.txt'

def foo(file1,file):
   file2=open(file2,"r")

def main():
 foo("C:\work.txt", "C:\vaction.txt")
petezurich
  • 9,280
  • 9
  • 43
  • 57
  • 1
    change the backslashes to forward slashes: `foo("C:/work.txt", "C:/vaction.txt")` or a pass a raw string `foo(r"C:\work.txt", r"C:\vaction.txt")` or escape the backslashes: `foo("C:\\work.txt", "C:\\vaction.txt")` – EdChum Mar 25 '19 at 14:23
  • this question has been asked before, see my answer here: https://stackoverflow.com/questions/54746407/getting-os-error-when-passing-string-to-pathlib-path-in-windows/54746518#54746518 – vencaslac Mar 25 '19 at 14:23
  • i thought there would be a better way to change the string inside the function i'm working on – eyal carmiel Mar 25 '19 at 14:30

2 Answers2

0

Either use raw strins r"C:\new.txt" or use double backslash "C:\\new.txt". I prefer using second method, because it is also used in system.

PS. I suppose you are on Windows

Maneren
  • 98
  • 1
  • 8
-1

You can use this:

import re
escaped = re.escape(yourString)

Best

Maxouille
  • 2,729
  • 2
  • 19
  • 42