0

I am trying to open a python file on my Windows computer with a subprocess, but I can't make it work.

import subprocess
subprocess.Popen('python C:\Users\Kristian\Desktop\Python\Ja\Bakkom var her.py',shell=True)

This is my code. when I run it I get a window on my screen saying:

(unicode error)'unicodeescape' codec can´t decode bytes in position 9-10: truncated \UXXXXXXXX escape

Bakkom
  • 57
  • 2
  • 9

1 Answers1

0

This is a famous problem with Windows, since it uses backslashes instead of forward as file delimiters. Python is trying to parse text preceded by \U (as in 'Users') as a Unicode character, which it isn't.

You can work around it by:

  • using double-slashes ('python C:\\Users\\Kristian\\Desktop\\Python\\Ja\\Bakkom var her.py')
  • using forward-slashes ('python C:/Users/Kristian/Desktop/Python/Ja/Bakkom var her.py')
  • prepending r to indicate raw text. (r'python C:\Users\Kristian\Desktop\Python\Ja\Bakkom var her.py')
Corey Goldberg
  • 59,062
  • 28
  • 129
  • 143
Josh Friedlander
  • 10,870
  • 5
  • 35
  • 75