0

How can I create a string that represents a Windows Path? I need to add a file that is generated dynamically to the end of the path as well. I have tried using raw strings but I can't seem to figure it out. Here's what I am trying to accomplish:

filename = "test.txt"
path = 'C:\Path\To\Folder\' + filename

Error I am seeing is: SyntaxError: EOL while scanning string literal

Sorry if this has been asked before I have tried looking at a few other SO questions and everyone reccomends using os.path.join but the problem there is I need to build a string, this code will not be running on a windows machine... Does that make a difference. Any help will be greatly appreciated!

Alex Daro
  • 439
  • 1
  • 7
  • 17

2 Answers2

7

You have a string literal, not a raw string. A raw string is a string literal with an r before it:

path = r'C:\Path\To\Folder\' + filename

Edit: Actually, that doesn't work because raw strings can't end with backslashes. If you still want to use a raw string, you could do one of the following:

path = r'C:\Path\To\Folder' + '\\' + filename
path = r'C:\Path\To\Folder\{}'.format(filename)

You could also double the backslashes:

path = 'C:\\Path\\To\\Folder\\' + filename

Yours didn't work because a backslash is a special character. For example, \n is not a backslash and an n; it is a new line. \' means to treat ' as a literal character instead of making it end the string. That means that the string does not end there, but you don't end it later in the line either. Therefore, when Python gets to the end of the line and you still haven't closed your string, it has an error. It is still better to use os.path.join(), though:

path = os.path.join(r'C:\Path\To\Folder', filename)
zondo
  • 19,901
  • 8
  • 44
  • 83
  • path = `r'C:\Path\To\Folder\' + filename` returns an error: **SyntaxError: EOL while scanning string literal** – Alex Daro Apr 12 '16 at 00:38
  • and with `path = 'C:\\Path\\To\\Folder\\' + filename` I get a string **'C:\\Path\\To\\Folder\\test.txt'** -- is the interpreter just printing this wrong? – Alex Daro Apr 12 '16 at 00:39
  • @DirkDigler: I'm amazed that the first throws an error. After a quick search, I found [this](https://stackoverflow.com/questions/647769/why-cant-pythons-raw-string-literals-end-with-a-single-backslash). It's incredible. As for the second, yes it's just how it's printed. If you use `print(path)`, it will show up normally, but if you just type `path` when you are in the shell, it will double all the backslashes. – zondo Apr 12 '16 at 00:43
  • and like i mentioned in the answer above's comments `path = os.path.join(r'C:\Path\To\Folder', filename)` returns - `'C:\\Path\\To\\Folder/test.txt'` – Alex Daro Apr 12 '16 at 00:43
  • very strange. `path = r'C:\Path\To\Folder\{}'.format(filename)` works in a Python Interpreter, but as soon as I run this code in my Flask application it returns with `'C:\\Path\\To\\Folder\\test.txt'` -- So it looks like this is a valid solution only if you `print`, but not if you want to use in live code. Any ideas? – Alex Daro Apr 12 '16 at 01:01
  • @DirkDigler: When you have backslashes in a string, you need to double them so that Python knows that you really mean backslash; you aren't trying to create a special character like newline, tab, form feed, etc. When you print that string, it shows up without the double backslashes. If you print it with `repr()`, as some things do, it will show up with the double backslashes. If you use that code for opening a file, it shouldn't have any problems. – zondo Apr 12 '16 at 01:04
  • when I double I am seeing `'C:\\\\Path\\\\To\\\\Folder\\\\test.txt'` – Alex Daro Apr 12 '16 at 01:08
  • @DirkDigler: Don't double them if you are using a raw string. Using a raw string is another way of doubling them. Just use the code that I gave in my answer. It should work fine. – zondo Apr 12 '16 at 01:09
  • That doesn't work either... I'm missing something here: `path = 'C:\\Path\\To\\Folder\\{}'.format(filename)` returns `'C:\\Path\\To\\Folder\\test.txt'` – Alex Daro Apr 12 '16 at 01:13
  • @DirkD: Have you tried using that path? It should work. – zondo Apr 12 '16 at 01:14
  • yes i tried using it, unfortunately doesn't work. Can you confirm that this code is working? `with open(path , 'wb') as csvfile:` – Alex Daro Apr 12 '16 at 01:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108870/discussion-between-dirkdigler-and-zondo). – Alex Daro Apr 12 '16 at 01:19
0
filename = "test.txt"
path =  os.path.join(r'C:\Path\To\Folder',filename)

although really python works fine using / as a file separator on any OS

Joran Beasley
  • 110,522
  • 12
  • 160
  • 179