Python: Accessing Windows mapped network drive with known URL, but unknown drive letter

前端 未结 4 1239
南方客
南方客 2021-02-10 07:21

I am trying to write a Python script that can move and copy files on a remote Linux server. However, I can\'t assume that everyone running the script (on Windows) will have mapp

相关标签:
4条回答
  • 2021-02-10 07:48

    Try using the short name instead of the fully qualified name. In you example that would be \\name-of-machine\var\SomeFile.txt.

    Edit:

    Okay, now I feel like a dummy -- hopefully you'll feel like one with me! ;)

    The machine name is name-of-machine.site.company.com, and the folder and file name is \var\SomeFile.txt -- what is the share name? In other words, your path should be something like:

    \\name-of-machine.site.company.com\share_name\var\SomeFile.txt
    
    0 讨论(0)
  • 2021-02-10 07:58

    Not a very elegant solution, but you could just try all the drives?

    From here:

    import win32api
    
    drives = win32api.GetLogicalDriveStrings()
    drives = drives.split('\000')[:-1]
    print drives
    

    Then you could use os.path.exists() on every drive:\var\SomeFile.txt until you find the right one.

    0 讨论(0)
  • 2021-02-10 08:00

    Easy solution is to use forward slashes to specify the Universal Name Convention (UNC):

    //HOST/share/path/to/file
    

    Found this solution in another thread and felt it would be relevant here. See original thread below:

    Using Python, how can I access a shared folder on windows network?

    0 讨论(0)
  • 2021-02-10 08:08

    If you can reserve a drive letter for this task, and you have the privileges then you can run "net use ..." from python and then use that fixed drive letter to read/write files.

    http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/net_use.mspx?mfr=true

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