Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving contents from a directory on a network drive (windows)

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.

like image 284
Verric Avatar asked Jan 16 '13 08:01

Verric


People also ask

How do I access files on a network drive?

Open My Computer and click on the Tools menu option. From the drop down list, choose Map Network Drive. Pick a drive letter that you want to use to access the shared folder and then type in the UNC path to the folder. UNC path is just a special format for pointing to a folder on another computer.

How do you access a file from the shared network drive?

Double-click the name of the computer from which the folder you want to open is being shared. Select a folder. Double-click the folder you want to open. Enter a username and password if prompted.

How do I navigate to a network folder?

Click File Explorer.Click This PC in the left side shortcut menu. Click Computer > Map network drive > Map network drive to enter Mapping wizard. Confirm drive letter to use (next available shows up by default).


2 Answers

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)

like image 139
Alex L Avatar answered Oct 21 '22 15:10

Alex L


This one worked for me:

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

(with Python 2.7 32b on Win7 64b)

like image 30
matekatona Avatar answered Oct 21 '22 14:10

matekatona