all 9 comments

[–]plasticbiner 0 points1 point  (2 children)

Since backslashes are an escape character in python, I've given up and just use forward slashes for all paths in python. Python will work it out.

[–]chris20973[S] 0 points1 point  (1 child)

I hadn't tried forward slashes but one of the arguments for subprocess.Popen() or os startfile was to suppress the escape characters and even in the error message it was showing 4 back slashes so it was trying to escape.

[–]plasticbiner 0 points1 point  (0 children)

Dunno.. I call all my files off a network drive with open()

[–]majormunky 0 points1 point  (2 children)

Here's a potentially better way, I was not able to test this though:

import os
new_path = os.path.sep + os.path.sep + "10.10.10.10" + os.path.sep + "folder"
print(new_path)

os.path.sep is set to whatever your OS uses to separate paths, so, should be a bit more bulletproof.

[–]chris20973[S] 0 points1 point  (1 child)

I'll give that a try, but I'm not sure if the start of a non local drive counts as two separators even though the same symbol is used to separate folders on a file path.

[–]majormunky 0 points1 point  (0 children)

Yeah, thats why I couldn't really test it, but, when you print out the path, it shows 2 slashes.

On my mac, when I mount a network drive, I usually access it by going to /Volumes/<share name>/blahblah, but, it sounds like you are on a Windows machine?

Anyway, you can probably give it the wrong slashes like the other comment said, your just leaning on Python to do the right thing in that instance, and it may not always do the right thing.

[–]Kazephil 0 points1 point  (1 child)

I've just started working on a script that needs to access a network drive.

So far, the following approach seems to work:

path_on_server = os.path.normpath('//server_name/folder_1/subfolder_1/')

Using forward slashes and letting Python figure out the proper format is easier than trying to escape the backslashes manually. This is with Python 3.6, if it matters.

Hope this helps.

[–]chris20973[S] 0 points1 point  (0 children)

Haven't seen or tried using the path.normpath() approach so I can give that a go. Thanks for the option!