89

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?

2
  • 8
    If you have correct permissions to access it, then I think regular open should work... Commented Aug 24, 2011 at 3:10
  • 2
    How can I do?. I have the username and password to the shared folder. What would be the code? Commented Mar 28, 2017 at 20:53

5 Answers 5

133

Use forward slashes to specify the UNC Path:

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

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

Sign up to request clarification or add additional context in comments.

9 Comments

This just solved a problem that was annoying me, thanks!
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?
@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')).
DavidJ Did you try @Johnsyweb solution? Did it work?
@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:
|
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')

.

Comments

10

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.

Comments

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.

Comments

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

1 Comment

f.close() should be in the finally block, or use with open('...path..') as f:

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.