0

So I have tried to read the solutions to Python duplicating a forwardslash from my code so it can find the file and most of the questions seem to indicate adding r' solves the problem.

In most of my code this works. But for this file path it is still duplicating all of the forwardslashes. Does anyone know why this would be the case?

I also tried using pathlib.Path to string together my path and it has produced the same result

For privacy I have removed the true file path but it is still replicating the issue. This is in my Jupyter Notebook.

additional backslash

  • 1
    Put a [mcve] *in the question*. You seem to be confusing forward and back slashes in your description. – jonrsharpe Feb 11 '19 at 16:11
  • 3
    Those are the same string. `r'\' == '\\'` – Dan D. Feb 11 '19 at 16:12
  • 3
    IMHE using `os.path.join` is usually the safest way – Tarifazo Feb 11 '19 at 16:13
  • 4
    Raw strings are just used for entering literals. When Python prints a string it doubles the backslashes since it doesn't use raw string format. – Barmar Feb 11 '19 at 16:14
  • The duplicate backslashes are not really in the string, there's nothing to worry about. – Barmar Feb 11 '19 at 16:15
  • Hi, it can't find the file path because it has duplicated them in the result? – Ryan Wilkinson Feb 11 '19 at 16:17
  • 1
    It does not duplicate the slashes. What you see in the log is the `repr` of the string, including `'...'` and all escape characters. – tobias_k Feb 11 '19 at 16:20
  • Note that the first backslash is indeed duplicated _in your raw-string_, i.e. `r'\\...'`. Could this be a simple typo? – tobias_k Feb 11 '19 at 16:22
  • Yes I understand it will print out the double slashes...however, it literally can't find the file because it seems to think there really are 2 slashes. And the first piece does have 2 slashes – Ryan Wilkinson Feb 11 '19 at 16:24
  • Note that Python lets you use forward slashes in the path, converting to a "proper" Windows path as needed. – chepner Feb 11 '19 at 16:28
  • Have you considered that the file might just not be there? What is the full path of the file, and what's the full path of the current execution directory of your script or IDE? – tobias_k Feb 11 '19 at 16:35

2 Answers2

1

"Raw strings" are the exact same type as regular strings, just a different way of entering them as input. Because their in-memory representation is identical, their "rawness" doesn't persist past the parser and change the way they behave later.

Thus, they still print the same way when repr()ed as any other string: You'll note that the representation didn't include the r'...' sigils, but was only '...'. As the way to represent r'\' as a non-raw-string is '\\', so the interpreter was correct to do so.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • Thank you Charles, so are you saying something is wrong with how the r' ' is in my code? – Ryan Wilkinson Feb 11 '19 at 16:28
  • @RyanWilkinson, your code is fine, and the Python interpreter is doing exactly what it should. All that's wrong is with your expectations. – Charles Duffy Feb 11 '19 at 16:29
  • Okay, so how do I get it to find my file path then? I've previously used the exact same path to access a different folder in that same drive and not had issues. What would you suggest as a resolution so Python can find my file? – Ryan Wilkinson Feb 11 '19 at 16:31
  • Ahh. So, *that's* a different question -- not about number of backslashes, but about being able to open UNC paths. Let's see if we have a duplicate for it already available... – Charles Duffy Feb 11 '19 at 16:32
  • @RyanWilkinson, ...best I can find from other questions on the topic, opening UNC paths in Python generally works fine. See f/e https://stackoverflow.com/questions/14354113/retrieving-contents-from-a-directory-on-a-network-drive-windows -- the OP had the same concern about the doubling of backslashes being part of their problem, but as described here, that's expected. – Charles Duffy Feb 11 '19 at 16:35
  • @RyanWilkinson, ...I'd suggest you start back up the tree -- see if your code can call `os.listdir(r'//na/na1')`, for example, and then walk *down* until you start getting errors. – Charles Duffy Feb 11 '19 at 16:36
  • Hi, I resolved this by adding 'Shared' in my file path. I have no idea why this was necessary as my other shared drives seem to work fine without it. Luckily I found an old script that had accessed a file in the same drive – Ryan Wilkinson Feb 11 '19 at 16:49
0

There was an absent file path that needed to be included

  • This answer explains your FileNotFoundError, but it doesn't address the question you asked, which is *Why is `r''` still duplicating the forward slashes in my code?*. – Charles Duffy Feb 12 '19 at 21:27
  • 'So I have tried to read the solutions to Python duplicating a forwardslash from my code so it can find the file and most of the questions seem to indicate adding r' solves the problem.' <--I clearly indicated I understood the python logic that I was having trouble in my particular instance – Ryan Wilkinson Feb 14 '19 at 23:06
  • Perhaps you might re-title your question so people who *don't* understand that logic don't end up looking in it for an answer to the question the title asks (but which the accepted answer doesn't address). If someone has to click through to read the body of a question before they have a good sense of whether its answers will solve their problem, the title isn't carrying its weight. – Charles Duffy Feb 14 '19 at 23:16