简体   繁体   English

检查网络驱动器是否在python中连接?

[英]Checking if network drive is connected in python?

I want to verify inside a python script running on Windows (7) that a network drive is connected and available.我想在 Windows (7) 上运行的 python 脚本中验证网络驱动器已连接且可用。 I would do something like the following:我会做类似以下的事情:

import os
connection_works = os.path.exists("F:\\")

However this seems to simply hang while the connection is down, returning True when the connection is reestablished.但是,这似乎只是在连接关闭时挂起,在重新建立连接时返回 True。 Using glob (my next idea) exhibits the same behavior.使用 glob(我的下一个想法)表现出相同的行为。 What would be the smart way of doing this?这样做的聪明方法是什么?

You're just saving the output from the os.path.exists() into connection_works.您只是将 os.path.exists() 的输出保存到 connection_works 中。 Try something like:尝试类似:

if os.path.exists("F:\\"):
    print('path exists'):
else:
    print('path does not exists):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM