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

前端 未结 3 886
耶瑟儿~
耶瑟儿~ 2020-12-14 20:48

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

path = "\\\\\\\\nexus\\\\File Server\\\\Technical\\\\MyDrive\\\\Software\\         


        
相关标签:
3条回答
  • 2020-12-14 21:18

    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)

    0 讨论(0)
  • 2020-12-14 21:19

    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.

    0 讨论(0)
  • 2020-12-14 21:31

    This one worked for me:

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

    (with Python 2.7 32b on Win7 64b)

    0 讨论(0)
提交回复
热议问题