Using Python, how can I access a shared folder on windows network?

175,925

Solution 1

Use forward slashes to specify the UNC Path:

open('//HOST/share/path/to/file')

(if your Python client code is also running under Windows)

Solution 2

How did you try it? Maybe you are working with \ and omit proper escaping.

Instead of

open('\\HOST\share\path\to\file')

use either Johnsyweb's solution with the /s, or try one of

open(r'\\HOST\share\path\to\file')

or

open('\\\\HOST\\share\\path\\to\\file')

.

Solution 3

I had the same issue as OP but none of the current answers solved my issue so to add a slightly different answer that did work for me:

Running Python 3.6.5 on a Windows Machine, I used the format

r"\DriveName\then\file\path\txt.md"

so the combination of double backslashes from reading @Johnsyweb UNC link and adding the r in front as recommended solved my similar to OP's issue.

Share:
175,925
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    I have a file that I would like to copy from a shared folder which is in a shared folder on a different system, but on the same network. How can I access the folder/file? The usual open() method does not seem to work?

  • Meelah
    Meelah over 8 years
    This just solved a problem that was annoying me, thanks!
  • DavidJ
    DavidJ almost 8 years
    This only works on Windows (yes, the question is tagged Windows, but accessing a Windows server from non-Windows OS may also be tagged as such). Anyone care to add a solution for other platforms (e.g. Linux) - if possible without something like Samba?
  • johnsyweb
    johnsyweb almost 8 years
    @DavidJ If you're using SMB on Linux, I'd expect //HOST/share/ to be mounted (somewhere like /mnt/share) and the file to be opened like a regular file (open('/mnt/share/path/to/file')).
  • Azy Sır
    Azy Sır over 5 years
    DavidJ Did you try @Johnsyweb solution? Did it work?
  • Azy Sır
    Azy Sır over 5 years
    @Johnsyweb have you got another solution for this somewhere? Possibly github so I can view and re-use? I'm on a MAC trying to hit a shared network address. It is mounted - I have followed all solutions on this place all are giving FileNotFoundError: [Errno 2] No such file or directory:
  • johnsyweb
    johnsyweb over 5 years
    @AzySır I'm afraid I do not. I would expect //HOST/share/ to be mounted (somewhere like /Volumes/share) and the file to be opened like a regular file (open('/Volumes/share/path/to/file')).
  • Azy Sır
    Azy Sır over 5 years
    @Johnsyweb my issue is im on OSX trying to open up a WINDOWS Network Server. It's saying not found even though it is evidently mounted - not sure how to proceed on it
  • yl_low
    yl_low almost 4 years
    if you are on MacOS, and you've mounted your server via Go > Connect to Server, you should be able to see your server if you use os.listdir("/Volumes/")
  • user3574939
    user3574939 over 3 years
    @AzySır I'm having a similar issue. Did you ever find a solution?