-1

I am trying to get the current working directory and add it to a file path with os.getcwd. Because windows uses forward slashes in the directory path, I need to change all of these slashes to back slashes for it to work in python.

What I've tried:

import os

old = getcwd()

new = old.replace("\", "/")

file_path = (new + "folder/filename")

print(file_path

The above is throwing an error of SyntaxError: EOL while scanning string literal

patriciajlim
  • 53
  • 2
  • 3
  • 9
  • 3
    "\" is also an escaping symbol. fix: `old.replace("\\", "/")` – Marat Aug 25 '20 at 15:53
  • 2
    Don't str concat `path`s, use `os.path.join`, or `pathlib.Path.joinpath` for that – han solo Aug 25 '20 at 15:56
  • "windows uses forward slashes": ``\`` is a _back_slash. `/` is a forward slash. A good way to remember which is which is to think about which direction the slash would fall if it was a real object. – Pranav Hosangadi Aug 25 '20 at 16:01
  • 1
    I am not sure why this question is closed, but the problem was a misunderstanding in the part of the questioner, and the right answer to the question is to use a modules that know how to work with paths, and the answer given by @Konrad Rudolph addresses and clarifies both – han solo Aug 25 '20 at 16:01
  • Thanks guys. Am new to python here so this is helpful – patriciajlim Aug 25 '20 at 16:11
  • 1
    Rather than a typo, this should be recognized as a duplicate of [How can I put an actual backslash in a string literal (not use it for an escape sequence)?](https://stackoverflow.com/questions/3380484). – Karl Knechtel Mar 15 '23 at 09:17

1 Answers1

4

Because windows uses forward slashes in the directory path

It doesn’t — it uses backslashes (but it also accepts forward slashes).

This works, regardless of operating system:1

import os

file_path = os.path.join(os.getcwd(), 'folder', 'filename')
# also works:
# file_path = os.path.join(os.getcwd(), 'folder/filename')

… but the specific error you’re getting is because you’re attempting to use an un-escaped backslash in a Python string. Since backslashes in strings have a special meaning, its usage needs to be escaped: use "\\" instead of "\". But as mentioned above, that’s irrelevant here (and 99% of the time when working with paths).


1 A cleaner approach would be via pathlib, which uses properly typed objects to encode paths, instead of strings:

import pathlib

file_path = pathlib.Path('.').absolute() / 'folder' / 'filename'
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
  • Thanks. Green question: would I still have to replace "\" with "/"? For context, I am using playsound to play sounds in my script and need python to be able to read the file path. – patriciajlim Aug 25 '20 at 16:22
  • @patriciajlim No. This should be clear from my answer: this is almost never needed when working with paths, and definitely not here. – Konrad Rudolph Aug 25 '20 at 16:23
  • Thank you. It worked. I am new to python, so I learned something new :) – patriciajlim Aug 25 '20 at 16:40