How to detect the presence of a html file

P

Phoe6

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.
True
False
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
 
K

Kent Johnson

Phoe6 said:
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.



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.
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.2

Kent
 
P

Phoe6

Kent said:
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
 
P

Peter Hansen

Kent said:
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
 
X

Xavier Morel

Phoe6 said:
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.

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
'\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")
 
B

BartlebyScrivener

Even weirder,

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

also seems to work.

How is that?
 
I

Irmen de Jong

BartlebyScrivener said:
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
 
P

Peter Hansen

Irmen said:
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
 
K

Kent Johnson

Peter said:
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
 
D

Dennis Lee Bieber

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

I suspect the os module is applying a normalization to the string
before passing it to the real system functions.
--
 
T

Tim Roberts

Irmen de Jong said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top