0

So, basically I'm trying to put a variable in a directory string, so here's what I got

os.startfile(r'C:\Users\User\Downloads\Alfred\start\\'+sentence+".exe")

But when I'm trying to run the script (the other code doesn't have anything to do with this error), this comes up:

    os.startfile(r'C:\Users\User\Downloads\Alfred\Open\\'+sentence+'.exe')
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\Users\\User\\Downloads\\Alfred\\Open\\\\badlion.exe'

It somehow counts "\\" as "\\\\", when I try to put "\" instead of "\\" it counts the variable (sentence), as a string??? Please help, I'm new to Python.

  • 1
    It's a raw string. \ in a raw string is \\ in a normal string. \\ in a raw string is therefore \\\\ – Zoe Oct 07 '20 at 06:55
  • can you print `sentence`? it seems as if it also contain `\\` – David Oct 07 '20 at 06:55
  • Basically, I'm making a virtual assistant, and it will do things for you, I'm positive I didn't accidentally say "backslash" –  Oct 07 '20 at 07:01
  • That's just the way backslashes are *represented* – juanpa.arrivillaga Oct 07 '20 at 07:02
  • I said "bob open badlion", and since I used code to remove "bob" and "open", it prints "badlion" –  Oct 07 '20 at 07:03
  • What is sentence? And can your run: print(type(sentence)) – Timur U Oct 07 '20 at 07:03
  • How about `f-strings`? `os.startfile(f"C:\Users\User\Downloads\Alfred\start\{sentence}.exe")` – PythonSherpa Oct 07 '20 at 07:04
  • , im not quite sure what that means –  Oct 07 '20 at 07:05
  • Anyway, the problem here is that even raw strings will do some primitive backslash processing, namely, for quote-chars, so, raw strings cannot end with an odd-number of backslashes. You can read about it at the bottom of [this section of the docs](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals) – juanpa.arrivillaga Oct 07 '20 at 07:05
  • PythonSherpa, a bunch of errors come up on my IDE Error: SyntaxError: EOL while scanning string literal –  Oct 07 '20 at 07:06
  • What version of Python are you using? – juanpa.arrivillaga Oct 07 '20 at 07:07
  • Now it says "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape" and it points to the "f" –  Oct 07 '20 at 07:09
  • Are you *trying* to put two backslashes there? Because that is, in fact, what you are doing – juanpa.arrivillaga Oct 07 '20 at 07:11
  • As I said in the post, if I put 1 backslash instead of 2, it contains the variable inside the string –  Oct 07 '20 at 07:13
  • **What are you trying to do**? And **again** what version of Python are you using? Note, `'C:\\Users\\User\\Downloads\\Alfred\\Open\\\\badlion.exe'` is a string with *two* backslashes there at the end, not 4. Go ahead and `print('C:\\Users\\User\\Downloads\\Alfred\\Open\\\\badlion.exe')` Did you want two? Or one? – juanpa.arrivillaga Oct 07 '20 at 07:14
  • I'm trying to have the script open a program, but as I said multiple times, if I put 1 backslash instead of 2 it will count the variable after it as a string, and if I don't have the r, it will also count the variable as a string. –  Oct 07 '20 at 07:19
  • @CraftYun83 I didn't ask you that, *I understand that*, **please answer my question**: do you want **one** or *two* backslashes? **What version of python are you using**. If it's *one* maybe the easiest is to use the answer by @TimurU but really, you should be using `pathlib` – juanpa.arrivillaga Oct 07 '20 at 07:20
  • I want 1 backslash because 2 backslashes are not a valid directory. Im using Python 3.8, and @TimurU's answer didn't works since there is 2 backslashes –  Oct 07 '20 at 07:28
  • @CraftYun83 no, Timur's solution **only has single backslashes**. Go ahead an `print('C:\\Users\\User\\Downloads\\Alfred\\start\\'+sentence+".exe")`, what do you see? – juanpa.arrivillaga Oct 07 '20 at 07:31
  • @CraftYun83 the representation of a string with a single backslash in Python produces two backslashes. __But the string itself still contains only one backslash__. – Błażej Michalik Oct 07 '20 at 07:51

3 Answers3

2

R string may give error with end symbol
But not error using \ - double slash

Use:

os.startfile('C:\\Users\\User\\Downloads\\Alfred\\start\\'+sentence+".exe")

But this type:

r’some text\’ 

may be error end of string

r’some text\\’ 

Will give double slash at end

Timur U
  • 415
  • 2
  • 14
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/222688/discussion-on-answer-by-timur-u-python-counts-2-backslashes-as-4). – Samuel Liew Oct 07 '20 at 23:56
1

If you want to workaround the problem with the \ at the end of your raw-string, in this case you may also use formatted-raw-strings:

os.startfile(fr'C:\Users\User\Downloads\Alfred\start\{sentence}.exe')

Provided, of course, the file under that path actually exists on your OS.

Błażej Michalik
  • 4,474
  • 40
  • 55
0

You have to understand the difference between raw string literals, normal strings, and how repr() deals with them.

raw = r"raw string: \ \\ \\\\"
normal = "normal string: \ \\ \\\\"

print(raw, repr(raw), normal, repr(normal), sep='\n')

Outputs:

raw string: \ \\ \\\\         
'raw string: \\ \\\\ \\\\\\\\'
normal string: \ \ \\         
'normal string: \\ \\ \\\\'   
  • Every slash in a raw string literal is treated as an actual slash
  • The path in the exception message is rendered using repr()

Therefore, each slash in your raw string literal will add two slashes in the exception message. But there's only one slash in the variable, and that's what python functions will see:

> print(raw[-1], normal[-1])

\ \
Błażej Michalik
  • 4,474
  • 40
  • 55