How to check if a path *could* be a legal path?

Discussion in 'Python' started by Maciej Sobczak, Apr 20, 2004.

  1. Hi,

    I have a string.
    This string is to be used as a path for a new file.
    I would like to check if this string *could be* a valid file name,
    *before* I try to create the file itself.
    In other words, I would like to know whether some string violates the
    underlying OS's policies (or maybe some more restriced policies, but
    portable) considering file paths.

    Example:

    1.
    s = 'C:\file.txt'

    the above is potentially a valid path on my system.

    2.
    s = 'cc:/^- =#jk\kj+.+?! :)'

    the above is rather invalid and I would like to check it somehow before
    I even try to create the file.

    Does the Python library contain a functionality that allows to achieve
    this goal?

    --
    Maciej Sobczak : http://www.msobczak.com/
    Programming : http://www.msobczak.com/prog/
    Maciej Sobczak, Apr 20, 2004
    #1
    1. Advertising

  2. Maciej Sobczak

    Gerrit Guest

    Maciej Sobczak wrote:
    > 1.
    > s = 'C:\file.txt'
    >
    > the above is potentially a valid path on my system.
    >
    > 2.
    > s = 'cc:/^- =#jk\kj+.+?! :)'
    >
    > the above is rather invalid and I would like to check it somehow before
    > I even try to create the file.
    >
    > Does the Python library contain a functionality that allows to achieve
    > this goal?


    No.
    It's different on different platforms.
    On Unix, for example, everything without \0 (ascii 0) is legal.
    I don't know the rules Windows needs, but apparantly they are more
    strict, or else the second path would be potentially valid as well.

    There may be a 3rd party package that has this functionality, however,
    or something in a windows-specific package.

    Can't you pass it to exists? Maybe os.path.exists makes a different
    between ENOENT and invalid paths...

    Gerrit.

    --
    Weather in Twenthe, Netherlands 20/04 12:25:
    12.0°C Scattered clouds partly cloudy wind 4.0 m/s SSW (57 m above NAP)
    --
    Experiences with Asperger's Syndrome:
    EN http://topjaklont.student.utwente.nl/english/
    NL http://topjaklont.student.utwente.nl/
    Gerrit, Apr 20, 2004
    #2
    1. Advertising

  3. Maciej Sobczak

    Larry Bates Guest

    It is much easier to put your code that would create the
    path inside a try block and just have an exception that
    handles the failure. If most of the time the path is
    legal, it is faster also.

    Regards,
    Larry Bates
    Syscon, Inc.

    "Maciej Sobczak" <> wrote in message
    news:c62ums$b3b$...
    > Hi,
    >
    > I have a string.
    > This string is to be used as a path for a new file.
    > I would like to check if this string *could be* a valid file name,
    > *before* I try to create the file itself.
    > In other words, I would like to know whether some string violates the
    > underlying OS's policies (or maybe some more restriced policies, but
    > portable) considering file paths.
    >
    > Example:
    >
    > 1.
    > s = 'C:\file.txt'
    >
    > the above is potentially a valid path on my system.
    >
    > 2.
    > s = 'cc:/^- =#jk\kj+.+?! :)'
    >
    > the above is rather invalid and I would like to check it somehow before
    > I even try to create the file.
    >
    > Does the Python library contain a functionality that allows to achieve
    > this goal?
    >
    > --
    > Maciej Sobczak : http://www.msobczak.com/
    > Programming : http://www.msobczak.com/prog/
    >
    Larry Bates, Apr 20, 2004
    #3
  4. Maciej Sobczak wrote:

    > [...]
    > 1.
    > s = 'C:\file.txt'
    >
    > the above is potentially a valid path on my system.
    >
    > 2.
    > s = 'cc:/^- =#jk\kj+.+?! :)'
    >
    > the above is rather invalid and I would like to check it somehow
    > before I even try to create the file.
    > [...]


    Your first string should actually be invalid on python since it will
    consider the "\f" to be a control-character. Thus "C:\file.txt" will be
    read as "C:<CTRL-F>ile.txt", which is not valid. However both
    "c:\\file.txt" and "c:/file.txt" are valid in python using the OS module
    on a windows system.

    Secondly, you could separate the file from the file path ( 'c:/' ,
    'file.txt' ), then check to see if the path is valid before creating the
    file.
    Gabriel Cooper, Apr 20, 2004
    #4
  5. Maciej Sobczak

    Peter Hansen Guest

    Maciej Sobczak wrote:

    > I have a string.
    > This string is to be used as a path for a new file.
    > I would like to check if this string *could be* a valid file name,
    > *before* I try to create the file itself.
    > In other words, I would like to know whether some string violates the
    > underlying OS's policies (or maybe some more restriced policies, but
    > portable) considering file paths.
    >
    > Does the Python library contain a functionality that allows to achieve
    > this goal?


    I don't think it does, and certainly not in a cross-platform manner.
    Furthermore, even on a given platform I suspect you will not find
    such a thing, because there are things like remote file systems
    which might change the rules. One file system might recognize
    more or fewer valid characters than another, even on the same OS.

    The best is probably just to define a restrictive subset of
    possible characters and do a simple regular expression which
    handles the basic path separator stuff, maybe with a little
    os.path.sep substituted on the fly to keep it relatively
    non-platform-specific.

    Presumably this isn't a case where you can just catch an exception
    at the time file creation is attempted? After all, even with
    a valid file name, you could still have several types of error
    at the moment of creation.

    -Peter
    Peter Hansen, Apr 20, 2004
    #5
  6. What is the problem with just creating the file and handling the error case with an except
    clause?

    Bill

    In article <c62ums$b3b$>, says...
    > Hi,
    >
    > I have a string.
    > This string is to be used as a path for a new file.
    > I would like to check if this string *could be* a valid file name,
    > *before* I try to create the file itself.
    > In other words, I would like to know whether some string violates the
    > underlying OS's policies (or maybe some more restriced policies, but
    > portable) considering file paths.
    >
    > Example:
    >
    > 1.
    > s = 'C:\file.txt'
    >
    > the above is potentially a valid path on my system.
    >
    > 2.
    > s = 'cc:/^- =#jk\kj+.+?! :)'
    >
    > the above is rather invalid and I would like to check it somehow before
    > I even try to create the file.
    >
    > Does the Python library contain a functionality that allows to achieve
    > this goal?
    >
    >
    Bill Rubenstein, Apr 20, 2004
    #6
  7. Maciej Sobczak

    Jorgen Grahn Guest

    [top-posting fixed]

    On Tue, 20 Apr 2004 09:07:51 -0500, Larry Bates <> wrote:
    > "Maciej Sobczak" <> wrote in message
    > news:c62ums$b3b$...
    >> Hi,
    >>
    >> I have a string.
    >> This string is to be used as a path for a new file.
    >> I would like to check if this string *could be* a valid file name,
    >> *before* I try to create the file itself.
    >> In other words, I would like to know whether some string violates the
    >> underlying OS's policies (or maybe some more restriced policies, but
    >> portable) considering file paths.


    > It is much easier to put your code that would create the
    > path inside a try block and just have an exception that
    > handles the failure. If most of the time the path is
    > legal, it is faster also.


    That strategy would fail in lots of situations where the path is actually
    well-formed, i.e. permission flags, non-existing directories, files where
    directories are expected ...

    But I agree that it generally seems better to detect failures here. And I
    think it's generally better to fail /hard/ here and let the user fix it,
    than try to fix the situation yourself - doing that would be awfully tricky
    in the general case.

    By the way, for Unix the test is pretty easy. It's more or less
    def isValid(path): return path and not '\0' in path

    /Jorgen

    --
    // Jorgen Grahn <jgrahn@ ''If All Men Were Brothers,
    \X/ algonet.se> Would You Let One Marry Your Sister?''
    Jorgen Grahn, Apr 24, 2004
    #7
  8. Maciej Sobczak

    Peter Hansen Guest

    Jorgen Grahn wrote:

    > By the way, for Unix the test is pretty easy. It's more or less
    > def isValid(path): return path and not '\0' in path


    Aren't there also length limits, and limits which differ
    depending on the specific *nix variant one has?

    -Peter
    Peter Hansen, Apr 24, 2004
    #8
  9. On Sat, 24 Apr 2004 08:52:31 -0400,
    Peter Hansen <> wrote:

    > Jorgen Grahn wrote:


    >> By the way, for Unix the test is pretty easy. It's more or less
    >> def isValid(path): return path and not '\0' in path


    > Aren't there also length limits, and limits which differ
    > depending on the specific *nix variant one has?


    And "/" is still reserved for the directory separator.

    Regards,
    Heather

    --
    Heather Coppersmith
    That's not right; that's not even wrong. -- Wolfgang Pauli
    Heather Coppersmith, Apr 24, 2004
    #9
  10. Maciej Sobczak

    Roger Binns Guest

    Heather Coppersmith wrote:
    > And "/" is still reserved for the directory separator.


    Unless you use older versions of PC-NFS in which case you
    could create files with '/' in them. You could never
    delete them from the server side though :)

    The easiest way of solving this whole problem is to
    copy how Microsoft does it! They create a new file/folder
    named "new file" or "new folder" and then put you in rename
    mode. If the user presses escape, delete the file,
    else rename it.

    Roger
    Roger Binns, Apr 25, 2004
    #10
    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. Roxanne
    Replies:
    0
    Views:
    1,230
    Roxanne
    Jul 4, 2003
  2. Replies:
    0
    Views:
    2,244
  3. Adam Knight
    Replies:
    1
    Views:
    1,120
    Ken Cox - Microsoft MVP
    Dec 5, 2005
  4. Replies:
    11
    Views:
    985
    Oliver Wong
    Sep 26, 2005
  5. Alessandro
    Replies:
    5
    Views:
    567
    Alessandro
    Sep 27, 2010
Loading...

Share This Page