No validation routine for an os.path

Discussion in 'Python' started by Edward Diener, Aug 17, 2004.

  1. 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 ?
    Edward Diener, Aug 17, 2004
    #1
    1. Advertising

  2. Edward Diener

    Peter Hansen Guest

    Edward Diener wrote:

    > 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
    Peter Hansen, Aug 17, 2004
    #2
    1. Advertising

  3. Peter Hansen wrote:
    > Edward Diener wrote:
    >
    >> 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.


    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.
    Edward Diener, Aug 18, 2004
    #3
  4. Edward Diener

    Peter Hansen Guest

    Edward Diener wrote:

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

    > Peter Hansen wrote:
    >>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.


    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
    Peter Hansen, Aug 18, 2004
    #4
  5. Edward Diener

    Roger Binns Guest

    Edward Diener wrote:
    > 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?



    fred!foovax!kremvax!nasa
    one@

    As a bonus question, are email addresses case sensitive?

    Roger
    Roger Binns, Aug 18, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. D. Shane Fowlkes

    just tinkering with a Sub Routine in ASP.NET

    D. Shane Fowlkes, Nov 12, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    424
    Scott M.
    Nov 13, 2003
  2. =?Utf-8?B?QmlsbCBCb3Jn?=

    Easiest way to get page name from error routine?

    =?Utf-8?B?QmlsbCBCb3Jn?=, Mar 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    434
    Tommy
    Mar 3, 2004
  3. Replies:
    1
    Views:
    420
  4. =?Utf-8?B?cm9kY2hhcg==?=

    my validation routine

    =?Utf-8?B?cm9kY2hhcg==?=, Jun 1, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    306
    =?Utf-8?B?cm9kY2hhcg==?=
    Jun 1, 2007
  5. dries

    credit card validation routine

    dries, Aug 13, 2004, in forum: Javascript
    Replies:
    9
    Views:
    138
    Michael Winter
    Aug 16, 2004
Loading...

Share This Page