Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
438 views
in Technique[技术] by (71.8m points)

python - 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

      \nexusFile ServerTechnicalMyDriveSoftwareReleases
    eleasemodule"
    

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 = "\\mysharefolder"
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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...