Reputation: 1281
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?
Upvotes: 87
Views: 285893
Reputation: 91
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).
Upvotes: 0
Reputation: 143
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.
Upvotes: 10
Reputation: 91
My remote server is on Linux Machine and the client on Windows. For me:
glob.glob('//HOST/share/path/to/file')
works with forward slashopen(r'\\HOST\share\path\to\file')
and open('\\\\HOST\\share\\path\\to\\file')
worked with backward slashpd.read_csv()
, forward or backward slash, doesn't matter.Upvotes: 1
Reputation: 141780
Use forward slashes to specify the UNC Path:
open('//HOST/share/path/to/file')
(if your Python client code is also running under Windows)
Upvotes: 133
Reputation: 91017
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')
.
Upvotes: 48