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

Jorgen Grahn jgrahn-nntq at algonet.se
Sat Apr 24 08:39:35 EDT 2004


[top-posting fixed]

On Tue, 20 Apr 2004 09:07:51 -0500, Larry Bates <lbates at swamisoft.com> wrote:
> "Maciej Sobczak" <no.spam at no.spam.com> wrote in message
> news:c62ums$b3b$1 at atlantis.news.tpi.pl...
>> 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?''



More information about the Python-list mailing list