19

I'm have an issue about displaying the files from a network drive on Windows.

path = "\\\\nexus\\File Server\\Technical\\MyDrive\\Software\\Releases\\%s\\%s\\" %(release, module)

where \\nexus\ is a network drive.

My main issue is given that a user enters correct variables, i'm unable to show the contents of the directory requested (the contents of 'module').

Things I've tried

  1. os.listdir(path)
    The issue with the line above is that it returns a windows error [123] which is, a can not find directory error. This is because listdir() seems to double all the back slashes resulting in :

     "\\\\\\\\nexus\\File Server\\\\Technical\\\\MyDrive\\\\Software\\\\Releases\\\\release\\\\module\\\\"
    
  2. print(glob.glob(path))
    I didn't really know exactly how it works :P but it seems just to display the directory supplied and not the contents of the ending directory

      \\nexus\File Server\Technical\MyDrive\Software\Releases\release\module\"
    

I've seen an os.walk however im not sure how its works, in that how does it defines what is the base directory /directories and what is the rest of the path

Extra notes: The contents of 'module' will always be a zip file, also the directory will generally contain at maximum five zip files.

Community
  • 1
  • 1
Verric
  • 1,044
  • 3
  • 11
  • 19
  • 4
    you should remove all the double slashes you have. use a raw string by putting `r` in front of it. so it should look like this: `r'\\nexus\File Server\Technical\MyDrive\Software\Releases\release\module\'` thats the first thing. try that. secondly, on windows, you can also use forward slashes, like this: `r'\\nexus/File Server/Technical/MyDrive/Software/Releases/release/module/'` but you still need the double backslash for a network resource. – Inbar Rose Jan 16 '13 at 08:37
  • 1
    @InbarRose: Not with the trailing backslash, that won't work.. `module\'` escapes that closing `'`, even in raw string literals. – Martijn Pieters Jan 16 '13 at 08:43
  • @MartijnPieters yes, thanks, its early in the morning and i still haven't finished my coffee :) – Inbar Rose Jan 16 '13 at 08:47
  • Can you simply map `\\nexus` to `Z:`? Similar question here: http://stackoverflow.com/questions/1459590/listing-network-shares-with-python – Alex L Jan 16 '13 at 08:48
  • Or could you include your domain? I.e. try `os.listdir('\\\\nexus.mydomain.com\File Server')` – Alex L Jan 16 '13 at 08:50
  • Try this out: path = "//nexus/File Server/Technical/MyDrive/Software/Releases/%s/%s/" %(release, module) – ATOzTOA Jan 16 '13 at 08:39
  • @ Inbar.R yeah actuallt providing the string as a raw one was one of the first things i did initially when creating this, howver i forgot to menton it above, all in all the it still results in doubling all the slashes if they are back slashes – Verric Jan 17 '13 at 09:50
  • @ Alex changing from \\nexus\ --> Z: still resulted in the same error, – Verric Jan 17 '13 at 09:51
  • @AlexL also thanks for revising my original question, also providing the full domain still returns the same "system can not find path" – Verric Jan 17 '13 at 09:53

3 Answers3

26

Just tested on my XP PC, Python 2.7, SMB share \\myshare

os.listdir('\\\\myshare') # Fails with "WindowsError: [Error 53] The network path was not found"

os.listdir('\\\\myshare/folder') # Succeeds

I think some of the confusion could be caused by WindowsError showing the repr() of the path, rather than the actual path -

>>> repr(path)
"'\\\\myshare'"
>>> str(path)
'\\myshare'

If this is a Python 3 & unicode problem, I suggest trying to fix the string first:

path = "\\\\myshare\folder"
path = bytes(path, "utf-8").decode("unicode_escape")
print os.listdir(path)

(unfortunately I can't test this since I don't have Python 3 installed, but please let me know if it works and I'll edit my answer)

Alex L
  • 8,748
  • 5
  • 49
  • 75
  • Intersting you should mention a unicode problem, IDLE (temporary python IDE complains unless i save the script encoded as utf-8. – Verric Jan 17 '13 at 10:01
  • Also since im not at work tomorrow i cant really test any new solutions or ideas until Monday, i can vpn into our network however im not sure how python will handle it Thanks for all replies, – Verric Jan 17 '13 at 10:03
  • @JohnSmith No worries, let me know how you go when you get a chance. I suggest trying to make sure that Python thinks `\\nexus\File Server\Technical` exists first, then you can worry about replacing strings :). (`isdir()` or `exists()` should help) – Alex L Jan 17 '13 at 10:07
  • 1
    What happen if the shared folder has credentials? I have the username and password. Where should I put these parameters? – GSandro_Strongs Mar 28 '17 at 21:31
  • @gs_developer_user3605534 - does http://stackoverflow.com/questions/28865050/python-can-i-suppy-username-and-password-to-os-listdir help? – Alex L Mar 30 '17 at 13:03
  • This is not working for python3, windows shared folder., can you please tell me revised solution? – Piyush S. Wanare May 04 '18 at 10:40
  • I was literally half day looking for this. Thanks a lot! – Newbie Dec 31 '21 at 00:21
4

This one worked for me:

os.listdir('\\\\server\folder\subfolder\etc')

(with Python 2.7 32b on Win7 64b)

Vinz
  • 5,997
  • 1
  • 31
  • 52
matekatona
  • 66
  • 3
0

The workaround for this issue is following:

os.listdir('\\networkshares\\folder1\\folder2\\folder3')

It means that you have to use double slashes instead of single one.

Viktor Malyi
  • 2,298
  • 2
  • 23
  • 40