473,830 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange os.path.exists( ) behaviour

os.path.exists( path) returns True if "path" exists

But on Windows it also returns True for "path" followed by any number of
dots :

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
import os
os.path.exists( 'Lib/os.py') True # expected os.path.exists( 'Lib/os.py.') True # unexpected os.path.exists( 'Lib/os.py.....') True # unexpected


Is there a reason for this ? Is there a test that returns True only for
the really existing path ?

Pierre
Jul 21 '05 #1
4 2996
Pierre wrote:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32

^^^^^^^
Here's the bug. You're using Windows. It's a filesystem, but not as we know it...

Anyway, You are getting exactly what the low-level Windows APIs return.

Here's a small "C" program. It prints "0" next to the filename if the
file exists, -1 otherwise, as described at
http://msdn.microsoft.com/library/de..._._waccess.asp

int main(int argc, char **argv) {
int i;
for(i=1; i<argc; i++) {
printf("%20s: %d\n", argv[i], _access(argv[i], 0));
}
}

I compiled it with "i386-mingw32msvc-gcc -o a.exe exist.c"

Here's an example session with it:
C:\TMP\example> dir
Volume in drive C has no label.
Volume Serial Number is 171D-4D2A

Directory of C:\TMP\example

07/06/05 03:04p <DIR> .
07/06/05 03:04p <DIR> ..
07/06/05 03:05p 3 exist
3 File(s) 3 bytes

C:\TMP\example> x:a.exe exist exist. exist.... nonexist nonexist. nonexist...
exist: 0
exist.: 0
exist....: 0
nonexist: -1
nonexist.: -1
nonexist...: -1

C:\TMP\example> type nonexist....
The system cannot find the file specified.

C:\TMP\example> type exist....
C:\TMP\example>

As you can see, not only does Windows think that "exist...." exists, but itcan
successfully "type" its contents too!

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFCzDn4Jd0 1MZaTXX0RAuCEAJ 9fcQp7EL7uXEnCn DD3iBkpFih5/gCfQ9Jd
uglZQylJ9n2ChXM UinFaACk=
=jFuL
-----END PGP SIGNATURE-----

Jul 21 '05 #2
Jeff Epler wrote:
Here's the bug. You're using Windows.


QOTW (speaking as someone who is using Windows right now).
--
Michael Hoffman
Jul 21 '05 #3
Jeff Epler <je****@unpytho nic.net> wrote:
Pierre wrote:
Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on wi=

n32
^^^^=
^^^
Here's the bug. You're using Windows. It's a filesystem, but not as we kn=
ow it...
[ ... ]
As you can see, not only does Windows think that "exist...." exists, but it=
can
successfully "type" its contents too!


The "reason" for this behaviour is that the concept of a file
extension is firmly embedded in the operating system. exists is a
file with no extension, as is exists. (no, it doesn't make a
distinction between "no extension" and "an empty extension").
From there it's not too great a leap of induction to suppose that
exists.... is the same file called exists with no extension. It's
not unlike being able to stick an arbitrary number of /s onto the
end of a directory name on a sane OS/filesystem.

--
\S -- si***@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
___ | "Frankly I have no feelings towards penguins one way or the other"
\X/ | -- Arthur C. Clarke
her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
Jul 21 '05 #4
Does this work differently under other platforms?

Pierre Quentel <qu************ @wanadoo.fr> wrote:
os.path.exists (path) returns True if "path" exists

But on Windows it also returns True for "path" followed by any number of
dots :

Python 2.4 (#60, Nov 30 2004, 11:49:19) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
import os
os.path.exists( 'Lib/os.py')True # expected os.path.exists( 'Lib/os.py.')True # unexpected os.path.exists( 'Lib/os.py.....')True # unexpected


Is there a reason for this ? Is there a test that returns True only for
the really existing path ?

Pierre


--
Regards,
Casey
Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
30416
by: Fernando Rodriguez | last post by:
Hi, How can I check for the xistence of any file that matches a wildcard? For example: ppis-*.iss os.path.exists() doesn't expand the wildcard...
0
1363
by: Robert Brewer | last post by:
I've got a webapp which is behaving oddly. I want to include an image on the webpage, but only if the physical file exists; if it doesn't, I don't want an img element to generate an ugly blank box. I know the physical location of the image(s) in question, and figured I'd use os.path.exists to see if each file exists. A quick test in the interpreter yields: >>> os.path.exists(u'S:/Scans/Projects/2004/TJ/04TJ0267.jpg') True
3
8990
by: Matt | last post by:
The user can select a file in the file open dialog using <input type="FILE">, but the user can also enter the full file path in the browse text box manually. My question is if there are JavaScript to check if the file path exists? please advise. thanks!!
3
1746
by: Matt | last post by:
Is there a way to use JavaScript to check if a file path exists? For example, user enter C:\eklrjlejr in a text box, and I want to check if the file really exists. Is there a way to use JavaScript to do that? please advise. thanks!!
3
8656
by: Matt | last post by:
<input type="file" size=50"> will produce the browse button and browse text box. The user can either select the file from browse button, or enter a path in browse text box manually. My question is if there are any client-side code to check if the path exists in this case? My understanding is that no client-side solution to this problem. please advise. thanks!!
1
1337
by: Matt | last post by:
There are no client-side javascript to check if the file path exists because the security reason. Correct? Because we don't want code to access our local machine from remote machine, that's where the page run from. Please advise. Thanks!!
1
5882
by: Peter Bienstman | last post by:
Hi, I'm trying to add support for unicode file names to my application. I'm running into problems with e.g. the os.path.exists routine, which complains if it gets passed a unicode string: Traceback (most recent call last): File "/usr/lib/python2.4/site-packages/mnemosyne/pyqt_ui/main_dlg.py", line 149, in fileNew
3
1594
by: klappnase | last post by:
Hi all, yesterday I installed Python-2.5 and python-2.4.4 on my windows2k box. When trying to use os.path.exists() to detect which drives are present on the system I noticed that these behave differently: Python 2.4.4 (#71, Oct 18 2006, 08:34:43) on win32 Type "copyright", "credits" or "license()" for more information.
0
1050
by: Steve Holden | last post by:
dudeja.rajat@gmail.com wrote: You could use one of the URL libraries. The os.path module is for dealing with the local filesystem. The absence of any exception from f = urllib.urlopen("http://www.google.com/webhp/") is a good indication that the addressed page "exists" (but remember that the web is a virtual address space, so it doesn't necessarily mean that
0
9780
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10769
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10476
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10520
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10196
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6940
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.