0

Why doesn't it work? my gut feeling is it has something to do with the slashes(\);

savepath = ("C:\\Python\" + date4filename + ".txt")

Error is

File "C:\python\temp.py", line 2
    savepath=("C:\\Python\" + date4filename)
                                           ^
SyntaxError: EOL while scanning string literal
[Finished in 0.191s]
3
  • The backslash escapes the closing quote. Commented Feb 6, 2020 at 21:29
  • 1
    you can use / for paths as well - its understood in win/linux and easier Commented Feb 6, 2020 at 21:31
  • Perhaps safer to use the path module for building file paths dynamically. Commented Feb 6, 2020 at 21:47

1 Answer 1

2

Back slash has special meaning which is used to take away special meaning of special characters when prefixed, here it is double quote ("). For this reason we have raw strings in python. Raw strings are defined using r' ' . When raw strings are used all characters inside string are treated normal with no special meaning

Since backslash has special meaning, to use actual backslash we need to use (\\)

savepath = ("C:\\Python\\" + date4filename + ".txt")

Not to make it complex, use os.path library

import os.path
os.path.join("c://python/", date4filename, ".txt")

To avoid these path problems, you can absolutely use *nix style forwardslash(/) in python regardless of platform

Sign up to request clarification or add additional context in comments.

2 Comments

wow ok, so for a single backslash use two and when two are needed use four lol that's me up and running now thank you @neotam
@usr2564301 removed r' '. Yes, we don't need both doubleslash(\) and raw string. I have missed, it was typo. Thanks. and, a note: raw strings won't help if string is ended with backslash (), still it gives EOL error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.