7

I am trying to write a Python script that can move and copy files on a remote Linux server. However, I can't assume that everyone running the script (on Windows) will have mapped this server to the same letter. Rather than prompting users for the correct letter, I want to simply access the server by its network URL, the one that the drive letter is mapped to. So, for instance, if I have mapped the server's URL

\\name-of-machine.site.company.com

To be drive S:\, I want to access, say, the file S:\var\SomeFile.txt in a drive-letter agnostic manner. I have looked around and the general recommendation seems to be to use UNC notation:

f = open(r"\\name-of-machine.site.company.com\var\SomeFile.txt", "w")

But if I try this, an IOError saying there is no such file or directory. If I try using the server's IP address instead (not the real address, but similar):

f = open(r"\\10.1.123.149\var\SomeFile.txt", "w")

I get, after a long pause, an IO Error: "invalid mode ('w') or filename". Why are these notations not working, and how can I access this server (ideally as if it were a local drive) by its URL?

dpitch40
  • 2,621
  • 7
  • 31
  • 44
  • The unc address should work--it works fine on my network. But if you don't have the correct permissions, then you will get an IOError. Sounds like a permissions problem. – MikeHunter Oct 04 '12 at 23:42

4 Answers4

7

Easy solution is to use forward slashes to specify the Universal Name Convention (UNC):

//HOST/share/path/to/file

Found this solution in another thread and felt it would be relevant here. See original thread below:

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

Community
  • 1
  • 1
UnNatural
  • 123
  • 3
  • 9
  • even so, a thumbs up from here, it matched by qoogle query to the letter. (and google brought me here so, thanks) - also, this solution does in fact solve the problem here, and I cannot see how this question differs – Henrik Feb 26 '15 at 11:17
5

Not a very elegant solution, but you could just try all the drives?

From here:

import win32api

drives = win32api.GetLogicalDriveStrings()
drives = drives.split('\000')[:-1]
print drives

Then you could use os.path.exists() on every drive:\var\SomeFile.txt until you find the right one.

Community
  • 1
  • 1
Junuxx
  • 14,011
  • 5
  • 41
  • 71
0

Try using the short name instead of the fully qualified name. In you example that would be \\name-of-machine\var\SomeFile.txt.

Edit:

Okay, now I feel like a dummy -- hopefully you'll feel like one with me! ;)

The machine name is name-of-machine.site.company.com, and the folder and file name is \var\SomeFile.txt -- what is the share name? In other words, your path should be something like:

\\name-of-machine.site.company.com\share_name\var\SomeFile.txt
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
  • No, doesn't work. I never reference the machine via the short name; mapping it as a network drive needs the fully qualified name. The fully qualified name is also a valid URL on my company's intranet. – dpitch40 Oct 04 '12 at 17:05
0

If you can reserve a drive letter for this task, and you have the privileges then you can run "net use ..." from python and then use that fixed drive letter to read/write files.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true

nagylzs
  • 3,954
  • 6
  • 39
  • 70
  • I don't want to worry about drive letters at all; isn't there some way to access the files on this server using the address I use to map the drive in the first place, rather than worry about arbitrary drive letters? Or do I have to do it via FTP or something? (Would that even work?) – dpitch40 Oct 04 '12 at 17:04
  • There are countless other ways to access files on a linux server. You can use DAV, for example. http://www.ikeepincloud.com/en/python_library But really there are so many protocols and client libs that is would be hard to list them all. I have selected webdav as an example because it does exactly what you need, and it is easy to install. Using windows networking is a nightmare anyway... – nagylzs Oct 04 '12 at 17:33