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

5

84

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?

Palmette answered 24/8, 2011 at 2:34 Comment(2)
If you have correct permissions to access it, then I think regular open should work...Enemy
How can I do?. I have the username and password to the shared folder. What would be the code?Concrescence
S
128

Use forward slashes to specify the UNC Path:

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

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

Slapdash answered 24/8, 2011 at 3:9 Comment(9)
This just solved a problem that was annoying me, thanks!Limey
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?Protagonist
@Protagonist 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')).Slapdash
DavidJ Did you try @Johnsyweb solution? Did it work?Duntson
@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:Duntson
@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')).Slapdash
@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 itDuntson
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/")Swagerty
@AzySır I'm having a similar issue. Did you ever find a solution?Subminiaturize
C
48

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')

.

Crept answered 24/8, 2011 at 5:19 Comment(0)
M
8

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.

Marmoreal answered 11/8, 2018 at 0:6 Comment(0)
S
1

My remote server is on Linux Machine and the client on Windows. For me:

  1. glob.glob('//HOST/share/path/to/file') works with forward slash
  2. open(r'\\HOST\share\path\to\file') and open('\\\\HOST\\share\\path\\to\\file') worked with backward slash
  3. For pd.read_csv(), forward or backward slash, doesn't matter.
Seasonal answered 17/8, 2022 at 6:42 Comment(0)
L
0

Slight twist on the other answers: I was testing and needed to check if a remote file existed or not. I found this code did the trick

try:  
    #  
    f = open(r'\\<server name>\<folder>\<filename>')  
    print("found file")  
    f.close()  
except:  
    print("file not seen")  

That 'close' is always a good idea (certainly in my case, where all I'm doing is checking presence).

Ludvig answered 17/12, 2023 at 23:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.