如何检查网络路径是否存在? [英] How to check if a network path exist?

查看:101
本文介绍了如何检查网络路径是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 linux 中使用 python 知道网络路径(例如//192.168.1.1/test)是否存在的最佳方法是什么?

What is the best way to know if a network path(e.g. //192.168.1.1/test) exist using python in linux?

推荐答案

如果路径"是指 Internet URL,则需要查看 urllib 模块.

If by "path" you mean an internet URL, you'll want to look at the urllib module.

from urllib import urlopen
try:
    urlopen(path)
except IOError:
    pass # does not exist
else:
    pass # does exist

如果通过路径"表示Windows UNC,则需要使用os模块.

If by "path" you mean a Windows UNC, then you'll want to use the os module.

import os
os.path.isdir(path)

注意,我发现Windows UNC路径有些不稳定.根据您的网络设置和权限,可能无法访问它们.

Note, I've found the Windows UNC paths somewhat flakey. Depending on your network setup and permissions, they may or may not be accessible.

这篇关于如何检查网络路径是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆