473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1923
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: David McCulloch | last post by:
QUESTION-1: How can I detect if Norton Internet Security is blocking pop-ups? QUESTION-2a: How could I know if a particular JavaScript function has been declared? QUESTION-2b: How could I...
3
by: andrew.neale | last post by:
Hello, I have a div that has an overflow set to auto. I would like to know if the scrollbars are visible in javascript. Any help is appreciated. Thanks
0
by: Michiel | last post by:
Hello, Can anyone give me an idea of what classes/interfaces I could use when I want to detect the presence of a virtual machine (VMware, Virtual PC) on a user's system ? Both managed and...
3
by: Richard Thornley | last post by:
Hello, I was just been given a project and I have some questions on how to accomplish the first part of the task. If a user sends an email to a specific email address I need to detect...
6
by: Perquiaga | last post by:
Hi to all I have to detect the mouse presence and disconection if occurs to give alerts. I'm using VB .NET, if somebody can help i'll apretiate it.
5
by: foolsbar | last post by:
Hi there, I'm trying to write a C# application that gets information from a machine such as operating system, memory, cpu etc. I'm using WMI for this, and it all seems to work fine apart from...
0
by: tsjwang | last post by:
I have a Windows XP system with a USB hub attached to it. The hub has a USB cdrom and a few USB storage devices plugged in. Occasionally the cdrom and/or the storage devices disappeared or failed to...
5
by: pamela fluente | last post by:
I have placed a button on a page to open a pdf stored on another site. When the user clicks on the button I do a Redirect to the pdf URL .... and this seems to work fine. Question: is this...
8
by: kroogar | last post by:
Hi gurus .Net 2.0 I'm on a project where I need to be able to detect if my clipboard contains BOTH images AND text (eg. when copying from a web page). For some reason...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.