如何检测html文件的存在 [英] How to detect the presence of a html file

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

问题描述

操作系统:Windows

Python版本:2.4

我的书下有bookmarks.html和wumpus.c:


当我试图检查bookmarks.html的存在时,我失败了。

Operating System: Windows
Python version: 2.4

I have bookmarks.html and wumpus.c under my c:

When I tried to check the presence of the bookmarks.html, I fail.

os.path.isfile(''c:\ bookmarks.html'')
False os.path.isfile(''c:\ wumpus.c'')
True

os.path.exists(''c:\ wumpus.c'')
真正的os.path.exists(''c:\ bookmarks.html'')
False

os.access(''c:\ bookmarks.html'',os.F_OK)
os.path.isfile(''c:\bookmarks.html'') False os.path.isfile(''c:\wumpus.c'') True
os.path.exists(''c:\wumpus.c'') True os.path.exists(''c:\bookmarks.html'') False
os.access(''c:\bookmarks.html'',os.F_OK)



False


我可以向你保证c:\ bookmarks.html存在!我打开了这个并且

也在浏览器中检查了它。


为什么会出现这种情况?我应该如何检查这个

文件的存在?


任何帮助表示赞赏。


谢谢!

Senthil


False

I can assure you that c:\bookmarks.html exists! and I opened this and
checked it in the browser as well.

Why is this behavior? And How Should I check for the presence of this
file?

Any help appreciated.

Thanks!
Senthil

推荐答案

Phoe6写道:
操作系统:Windows < Python版本:2.4

我在我的c下有bookmarks.html和wumpus.c:

当我试图检查bookmarks.html的存在时,我失败。

Operating System: Windows
Python version: 2.4

I have bookmarks.html and wumpus.c under my c:

When I tried to check the presence of the bookmarks.html, I fail.

os.path.isfile(''c:\ bookmarks.html'')
错误
os.path.isfile(''c:\wumpus.c'')
True
os.path.isfile(''c:\bookmarks.html'')
False
os.path.isfile(''c:\wumpus.c'')
True




问题是\是特殊的在字符串文字中。 \b是退格

字符,而不是您期望的双字符序列。 \ w没有特殊的

意思是它*是你期望的双字符序列。



The problem is that \ is special in string literals. \b is a backspace
character, not the two-character sequence you expect. \w has no special
meaning so it *is* the two-character sequence you expect.

len(''\ b'')
1 len(''\ w'')
2

最简单的解决方法是使用原始字符串满足所有Windows路径需求:

os.path.isfile(r''c:\ bookmarks.html'')

os.path.isfile(r''c:\ wumpus.c''' )


在原始字符串中,唯一的\转义符是\''和\,其他所有内容都是单独的
。 len(r''\'')
2 len(r''\w'')
len(''\b'') 1 len(''\w'') 2

The simplest fix is to use raw strings for all your Windows path needs:
os.path.isfile(r''c:\bookmarks.html'')
os.path.isfile(r''c:\wumpus.c'')

In raw strings the only \ escapes are \'' and \", everything else is left
alone. len(r''\b'') 2 len(r''\w'')



2


Kent


2

Kent


Kent Johnson写道:
Kent Johnson wrote:
问题是\在字符串文字中很特殊。 \b是一个退格的
字符,而不是您期望的双字符序列。 \ w没有特殊含义,所以它*是你期望的双字符序列。
最简单的解决方法是使用原始字符串来满足所有Windows路径需求:
os.path.isfile(r''c:\ bookmarks.html'')
os.path。 isfile(r''c: \wumpus.c'')
The problem is that \ is special in string literals. \b is a backspace
character, not the two-character sequence you expect. \w has no special
meaning so it *is* the two-character sequence you expect. The simplest fix is to use raw strings for all your Windows path needs:
os.path.isfile(r''c:\bookmarks.html'')
os.path.isfile(r''c:\wumpus.c'')




非常感谢,肯特!

我立即认出了这个问题来自你的回复。

我刚开始使用python。 :)


再次感谢!


问候,

Senthil



Thanks a lot, Kent!
I immediately recognized the problem from your reply.
I am just starting with python. :)

Thanks again!

Regards,
Senthil


Phoe6写道:
操作系统:Windows
Python版本:2.4

我有bookmarks.html和wumpus.c下我的c:

当我试图查看bookmarks.html的存在时,我失败了。
Operating System: Windows
Python version: 2.4

I have bookmarks.html and wumpus.c under my c:

When I tried to check the presence of the bookmarks.html, I fail.
os.path .isfile(''c:\ bookmarks.html'')False os.path.isfile(''c:\ wumpus.c'')True
os.path.exists(''c:\\ \\ wumpus.c'')真正的os.path.exists(''c:\ bookmarks.html'')错误
os.access(''c:\ bookmarks.html'',os.F_OK )错误

我可以向你保证c:\ bookmarks.html存在!我打开了这个并在浏览器中检查了它。

为什么会出现这种情况?我该如何检查这个
文件的存在?

任何帮助表示感谢。

谢谢!
Senthil
os.path.isfile(''c:\bookmarks.html'') False os.path.isfile(''c:\wumpus.c'') True
os.path.exists(''c:\wumpus.c'') True os.path.exists(''c:\bookmarks.html'') False
os.access(''c:\bookmarks.html'',os.F_OK) False

I can assure you that c:\bookmarks.html exists! and I opened this and
checked it in the browser as well.

Why is this behavior? And How Should I check for the presence of this
file?

Any help appreciated.

Thanks!
Senthil



你试过逃避\吗?


尝试



Have you tried escaping the "\"?

try

os.path.exists('' c:\\bookmarks.html'')
os.path.exists(''c:\\bookmarks.html'')



''\ w''不是一个特殊的序列,因此自动获取

翻译为转义的\\\\,但是\\\"等同于\ x08并且

你的函数因此看到字符串c; \ x08ookmarks.html。


如果你不想逃避你的字符串,使用rawstrings(将你的

字符串添加到r,c:\ bookmarks.html因此变为

r" c:\ bookmarks.html" )



''\w'' is not a special sequence and therefore gets automagically
translated to the escaped "\\w", but "\b" is equivalent to "\x08" and
your functions therefore see the string "c;\x08ookmarks.html".

If you don''t want to escape your strings, use rawstrings (prepend your
strings with "r", "c:\bookmarks.html" therefore becomes
r"c:\bookmarks.html")


这篇关于如何检测html文件的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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