4

I am using python v3.6 on Windows 10. When specifying a string to represent a directory location, what is the difference between the 2 approaches below?

folder_location = 'C:\\Users\\username\\Dropbox\\Inv'
folder_location = 'C:/Users/username/Dropbox/Inv'

This is a follow-up question to another question I just posted. My problem was solved when I used \\ instead of /.

What is wrong with this selenium firefox profile to download file into customized folder?

user1315789
  • 3,069
  • 6
  • 18
  • 41
  • Most Windows APIs accept paths with either forward or backward slash as a delimiter delimiter. This is not Python specific, but it is easier to specify forward slashes in Python literals, as backward slashes need to be escaped. Hence the double backslash. – Martijn Pieters Oct 18 '17 at 08:09
  • always use the native path separator, some programs may not like the "/" in windows, even if the filesystem knows how to handle those. note that you can use the raw prefix instead of doubling the backslashes – Jean-François Fabre Oct 18 '17 at 08:10
  • Possible duplicate of [mixed slashes with os.path.join on windows](https://stackoverflow.com/questions/16333569/mixed-slashes-with-os-path-join-on-windows) – casraf Oct 18 '17 at 08:10
  • @Jean-FrançoisFabre: I don't know exactly which APIs in Windows don't accept forward slashes, but I believe they are no longer that relevant in Python 3. – Martijn Pieters Oct 18 '17 at 08:11
  • Why the 3 negative votes? What's wrong with the question? I would like to know to improve my future questions on stack overflow. Thank you. – user1315789 Oct 18 '17 at 08:11
  • @MartijnPieters I meant... exactly like the upvoted answer. If the app doesn't expect slashes, then it will fail. It's bad design but using backslashes ensures that your config work with all software – Jean-François Fabre Oct 18 '17 at 08:21
  • Not all software. Probably more software entry fields in Windows, but certainly not in e.g. config files where it is frequently an escape character. Sticking to assumptions like these gets us [repeated system crashing exploits](https://www.bleepingcomputer.com/news/microsoft/filesystem-bug-hangs-or-crashes-windows-7-and-windows-8-1/). – Yann Vernier Oct 18 '17 at 08:35

5 Answers5

6

On Unix systems, the folder separator is /, while on Windows systems, the separator is \. Unfortunately this \ is also an escape character in most programming languages and text based formats (including C, Python and many others). Strangely enough a / character is not allowed in windows paths.

So Python on windows is designed to accept both / and \ as folder separator when dealing with the filesystem, for convenience. But the \ must be escaped by another \ (unless of course you use raw strings like r'backslashes are now normal characters \\\ !')

Selenium, on the other hand, will write values into Firefox preferences, which, unlike Python, expects the appropriate kind of separator. That's why using forward slashes does not work in your example.

Guillaume
  • 5,497
  • 3
  • 24
  • 42
  • Thanks a lot. This answers my puzzle why it `/` didn't work on Selenium but worked on my past python scripts. I will mark your answer as the correct answer. I am sorry I do not have enough votes to upvote it. – user1315789 Oct 18 '17 at 08:16
  • 1
    It is not Python that is designed this way. The Windows APIs themselves accept paths with forward slashes. Python just accommodates for that reality. – Martijn Pieters Oct 18 '17 at 09:01
2

Windows uses by default backslashes as file/folder seperator the \\ is an escaped \. The POSIX compliant file/folder seperator / is also supported by the windows api. But the library you use (which is not recognizable in your example) need also support it.

zwobot
  • 31
  • 4
1

Windows inherited backslashes as a path separator from Microsoft DOS. DOS initially didn't support subdirectories and opted to use the (on US keyboards) easily typed slash / character for command line switches.

When they did introduce subdirectories in DOS 2, either slash / or backslash \ worked as a path separator, but to use slashes on the command line you had to reconfigure the switch character, a feature they later removed entirely.

Thus the command line for certain commands that look for switches without space in front (like dir/w) is the one place you can't use forward slashes (this has to do with the command line being passed as a single string, unlike POSIX which passes distinct arguments in a list). That, and poorly written code that tries things like splitting on backslash, not knowing that slash is also a path separator.

It's also sometimes complicated by either character having other meanings, such as \ being the escape character in string literals; that's why you use \\ unless you use a raw string r'foo\bar'.

The other path separator I know of is classic Mac OS, which uses colon :. Python handles these differences by including reasonable routines in os.path or pathlib.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Yann Vernier
  • 15,414
  • 2
  • 28
  • 26
1

The standard Windows path separator is backslash \. But it is used in string formatting so for example \n is end of line.

For the above reason you rather don't want to use backslash in you path as if the name of the folder will start with a letter corresponding to special characters you will run into troubles.

To use native backslash separator in windows you have two ways. Yo can use raw string and then all special characters are read literary. path = r"C:\user\myFolder" or escape backslach with escape character with turns out to be the backslash too path = "C:\\user\\myFolder".

But coming back to DOS it accepted forward slash in path string too Python is able to accept both separators. It is advised to use native way of formatting on your system

If you want you script working on both systems try:

import os
if os.name == 'posix':
    path = '/net/myFolder/' 
else:
    path = r'C:\Users\myFolder'
tomasz74
  • 16,031
  • 10
  • 37
  • 51
-1

Windows and Linux/macOS use different path separators - UNIX uses forward slashes (/) while Windows use back slashes (\).

You should never type your own separators, always use os.path.join or os.sep, which handle this for you based on the platform you're running on. Example:

import os

folder_location = os.path.join('C:\\', 'Users', 'username', 'Dropbox', 'Inv')
# or
folder_location = os.sep.join(['C:\\', 'Users', 'username', 'Dropbox', 'Inv']);

Also, you will need to manually escape the drive letter's trailing slash manually, as specified on the Python docs:

Note that on Windows, since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.

Hard-coding a full path like this is usually useless, as C: will only work on Windows anyway. You will most likely want to use this later on using relative paths or paths that were fetched elsewhere and need to have segments added to them.

casraf
  • 21,085
  • 9
  • 56
  • 91
  • Thanks for the answer. The best practice is to use os.path,join for platform compatibility. Good to know that. I am sorry I do not have enough votes to upvote your answer. – user1315789 Oct 18 '17 at 08:19
  • joining with a letter drive doesn't work. and I don't think folks will like to split their pasted paths to make it look like that. – Jean-François Fabre Oct 18 '17 at 08:19
  • As I specified, you wouldn't usually actually want to use the full path directly anyway. As for the drive letter, I updated with a fix – casraf Oct 18 '17 at 08:21
  • 1
    You should use `os.path.normpath('c:/Users/username/dropbox/inv')` instead. https://docs.python.org/2/library/os.path.html#os.path.normpath – Guillaume Oct 18 '17 at 08:21
  • For fun: `c:` and `C:` are actually different drives on some Atari systems (cartridge and hard drive). – Yann Vernier Oct 18 '17 at 08:38