简体   繁体   中英

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. 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. Using glob (my next idea) exhibits the same behavior. What would be the smart way of doing this?

You're just saving the output from the os.path.exists() into connection_works. Try something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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