469,289 Members | 2,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

How to detect the presence of a html file

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') 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

Dec 9 '05 #1
10 1772
Phoe6 wrote:
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


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

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
Dec 9 '05 #2
Kent Johnson wrote:
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')


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

Thanks again!

Regards,
Senthil

Dec 9 '05 #3
Phoe6 wrote:
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') 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')


'\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")
Dec 9 '05 #4
Kent Johnson wrote:
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')


Simpler still is almost always to use forward slashes instead:

os.path.isfile('c:/bookmarks.html')
os.path.isfile('c:/wumpus.c')

The only time this doesn't really work is when you pass the path to the
Windows "shell". Or when you naively compare paths without using
something like normpath() on them first...

-Peter

Dec 9 '05 #5
Even weirder,

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

also seems to work.

How is that?

Dec 9 '05 #6
> Even weirder,
os.path.isfile(r'c://bookmarks.html')


Never mind. It works that way from the command line, too. Never tried
it before.

rd

Dec 9 '05 #7
BartlebyScrivener wrote:
Even weirder,

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


Never mind. It works that way from the command line, too. Never tried
it before.


Forward slashes as path separator only works on NTFS volumes I believe.

--Irmen
Dec 9 '05 #8
Irmen de Jong wrote:
Forward slashes as path separator only works on NTFS volumes I believe.


I'm not sure what they *don't* work on, but at the least they also work
across the network as in:

os.listdir('//server/shared/xfer')

Maybe that still qualifies as "NTFS"...

-Peter

Dec 10 '05 #9
Peter Hansen wrote:
Kent Johnson wrote:
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')

Simpler still is almost always to use forward slashes instead:

os.path.isfile('c:/bookmarks.html')
os.path.isfile('c:/wumpus.c')

The only time this doesn't really work is when you pass the path to the
Windows "shell". Or when you naively compare paths without using
something like normpath() on them first...


I often copy paths from Windows Explorer and paste them into Python
strings. (I hate typing paths!) Raw strings are much handier in that
case - no editing required on the path.

Kent
Dec 10 '05 #10
Irmen de Jong <ir**********@xs4all.nl> wrote:
BartlebyScrivener wrote:
Even weirder,

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


Never mind. It works that way from the command line, too. Never tried
it before.


Forward slashes as path separator only works on NTFS volumes I believe.


Wrong. They work on all Microsoft operating systems back to at least
MS-DOS 5, and on every version of Windows.

The command interpreters don't accept them, but all of the APIs do.
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Dec 10 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

23 posts views Thread by David McCulloch | last post: by
3 posts views Thread by andrew.neale | last post: by
reply views Thread by Michiel | last post: by
3 posts views Thread by Richard Thornley | last post: by
6 posts views Thread by Perquiaga | last post: by
5 posts views Thread by foolsbar | last post: by
5 posts views Thread by pamela fluente | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.