4

I tried to search a lot on the forums but just couldn't find the right answer.

One of my python scripts, which runs on a windows machine looks for the existence of a network path as the first thing.

myPath = "Y:\\Windows\\Builds\\"
if not os.path.exists(myPath):
    print("This one can't be reached : "+myPath)

This works perfectly fine from a windows 7 machine (The output is true). But running the same on a windows 10 machine, results in false. All three machines, the windows 7 one, windows 10 one and Y: (a mac) are on the same local network.

Y: is a mapped drive. I have also tried to repeat with the IP instead of the mapped drive name, without luck. I have checked the paths are correct knowing the command is case sensitive.

Any help here will be highly appreciated. Thanks.

Rohit Singhal
  • 59
  • 1
  • 2
  • What happens if you try to stat the file? `os.stat(myPath)`. – Dunes Oct 15 '15 at 10:31
  • Try `os.path.join(Windows,Builds)` – Ravichandra Oct 15 '15 at 10:34
  • Mapped drives are created per logon session. If you have UAC enabled, then be aware that anything you do while elevated (i.e. "run as administrator") is in a different logon session. So if you map a drive in Explorer, it won't be available in an elevated command prompt; you'll have to remap it using `net use [A-Z]: [\\computername\sharename]`. – Eryk Sun Oct 15 '15 at 11:28
  • @Dunes, if I try os.stat, it returns this FileNotFoundError: [WinError 3] The system cannot find the path specified: 'Y:\\Windows\\Builds\\' – Rohit Singhal Oct 16 '15 at 09:39
  • @Ravichandra, I tries he os.path.join, but with the same response – Rohit Singhal Oct 16 '15 at 09:42
  • @eryksun, I have mounted the drives as the same user. And I am running the script as the same user. So it should be the same logon session. Just to avoid the confusion, I also tried the full path with the IP of the machine. That also didn't work – Rohit Singhal Oct 16 '15 at 09:42
  • OK, it's not working for other reasons. But just FYI, under UAC an administrator is logged on with a restricted token, which is not the same logon session as the unrestricted token used by elevated processes. – Eryk Sun Oct 16 '15 at 09:57
  • Just for giggles, navigate in `cmd` to the target directory and type `python -c 'import os; print os.getcwd()'` in both Windows 7 and 10. – dawg Dec 15 '15 at 05:32

2 Answers2

2

You may try os.path.join() to join paths:

path = os.path.join("Y:","windows","Build")

This will create a path string with regard to OS - for Windows7 windows\\Build and for Linux windows/Build.

use os.path.isdir(path) Return True if path is an existing directory.

Ravichandra
  • 2,162
  • 4
  • 24
  • 36
  • I tries he os.path.join, but with the same response. I am trying to access the shared files on a mac from a windows 10 machine via the python script. – Rohit Singhal Oct 16 '15 at 09:43
  • try `os.path.isdir()` – Ravichandra Oct 16 '15 at 10:37
  • So os.path.isdir returns 'True' and os.path.exists returns 'False. What could be the cause? I can access the path manually via windows explorer. – Rohit Singhal Oct 16 '15 at 10:50
  • @RohitSinghal Is `os.path.isdir()` working ?? IFF because `path.isdir()` is platform independent. check http://stackoverflow.com/questions/15077424/python-os-path-exists-vs-os-path-isdir check `octoblack` answer – Ravichandra Oct 16 '15 at 13:42
  • thanks for pointing me to the post. However, I still couldn't fix my problem. I do understand the platform independent thing of 'isdir'. But, if 'exists' returns false, my shutil.copy2 from that path fails as well. Could you point me to a platform independent copy command? – Rohit Singhal Oct 20 '15 at 03:02
  • try `os.system(cp source_path destination_path)`. I am not sure weather will it work or Not in your case !! – Ravichandra Oct 20 '15 at 09:30
  • @rohitSinghal Try to reach source path from cmd prompt and use same. – Ravichandra Oct 20 '15 at 09:35
  • @ravicandra I could get it working using the combination of os.path.isdir() and the cp command. please post it as an answer and I'll accept it. – Rohit Singhal Dec 15 '15 at 05:17
  • @RohitSinghal ok Thank you !! – Ravichandra Dec 15 '15 at 05:20
  • Another option is to build your path like so: \\\\servername\\folder\\folder\\file.txt - Essentially you have to escape the slashes – eliteproxy Feb 28 '19 at 21:04
1

If it is a network share you have to use the full path e.g.:

from pathlib import Path

myPath = Path('//server/sharename/Windows/Builds/')
if not os.path.exists(myPath):
    print("This one can't be reached : " + myPath)
Mane
  • 21
  • 3