No validation routine for an os.path

E

Edward Diener

Try as I might I can not find a routine in os.path which validates whether
or not a path is syntactically valid, either as a directory or as a file.
This is surprising since, although I know this is OS dependent, Python has
many other classes and functions which will work properly depending on what
OS they are currently running under. Is there such a path validation routine
in any of the libraries distributed with Python or in any other 3rd party
Python libraries ?
 
P

Peter Hansen

Edward said:
Try as I might I can not find a routine in os.path which validates whether
or not a path is syntactically valid, either as a directory or as a file.
This is surprising since, although I know this is OS dependent, Python has
many other classes and functions which will work properly depending on what
OS they are currently running under. Is there such a path validation routine
in any of the libraries distributed with Python or in any other 3rd party
Python libraries ?

I don't really think so, and the last time this sort of thing
was discussed, I believe the conclusion was that it's not
actually possible to do such a thing in all cases anyway,
unless you are willing to actually attempt to create the
file or directory during the validation.

One reason I recall being given is that even on a given OS, different
file systems may support different sets of characters for filenames.
For example, a CDROM might disallow certain characters which the
standard file system allows, or a networked device (e.g. NFS, or
Samba) might allow a wider range of possibilities than the permitted
names on a hard drive.

In the end, this is somewhat like validating email addresses. Until
you actually try and succeed (or fail) to send the message, you cannot
verify whether an address is "valid" for many definitions of valid.

-Peter
 
E

Edward Diener

Peter said:
I don't really think so, and the last time this sort of thing
was discussed, I believe the conclusion was that it's not
actually possible to do such a thing in all cases anyway,
unless you are willing to actually attempt to create the
file or directory during the validation.

Operating systems do have rules for what constitutes a valid file
specification and/or directory specification. Some even have API functions
which can be called to directly find out.
One reason I recall being given is that even on a given OS, different
file systems may support different sets of characters for filenames.
For example, a CDROM might disallow certain characters which the
standard file system allows, or a networked device (e.g. NFS, or
Samba) might allow a wider range of possibilities than the permitted
names on a hard drive.

You have a good point there. But with a file spec and a bit of work, one
should be able to tell what sort of drive is being accessed.
In the end, this is somewhat like validating email addresses. Until
you actually try and succeed (or fail) to send the message, you cannot
verify whether an address is "valid" for many definitions of valid.

I do not totally agree, especially as the OS will know whether something is
valid so it obviously has rules itself. But I grant it is more difficult
than might be assumed. BTW email addresses actually do have a format which
can be parsed ( RFC 822 originally ), although again it is complicated.
 
P

Peter Hansen

Edward said:
Operating systems do have rules for what constitutes a valid file
specification and/or directory specification. Some even have API functions
which can be called to directly find out.

True, but with dynamically loadable extensions to the OS, it
seems unlikely that the builtin API for checking filename
validity will necessarily handle a completely new filesystem
that was created after the OS was released...
I do not totally agree, especially as the OS will know whether something is
valid so it obviously has rules itself.

Again, the OS might have rules, but they can't always apply
to things that are not really part of the OS, as an add-on
might be. I'm sure that in most cases the add-on would not
be expanding the set of allowable characters, but it's
certainly not unusual for the set to be narrowed.
> BTW email addresses actually do have a format which
can be parsed ( RFC 822 originally ), although again it is complicated.

I was careful to say "for many definitions of valid". Valid might
be as trivial as checking whether it matches a particular format
(though I think the term "legal" might be more usual there), but
it could also mean "the address actually exists and can receive
mail" in which case you really can't know for sure unless you
try.

In the end, however, I still think the answer to your query is
an unfortunate "sorry, not available".

-Peter
 
R

Roger Binns

Edward said:
I do not totally agree, especially as the OS will know whether
something is valid so it obviously has rules itself. But I grant it
is more difficult than might be assumed. BTW email addresses actually
do have a format which can be parsed ( RFC 822 originally ), although
again it is complicated.

I don't think Peter meant whether or not you can extract an email
address, but rather if it is actually valid. For example,
which of these are valid?

(e-mail address removed)
(e-mail address removed)
fred!foovax!kremvax!nasa
one@[email protected]

As a bonus question, are email addresses case sensitive?

Roger
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top