[Tutor] valid filenames

Michael P. Reilly arcege@dsl254-114-246.nyc1.dsl.speakeasy.net
Sun, 1 Apr 2001 08:45:35 -0400 (EDT)


Scott wrote
> Is there a Python module that contains a function that determines if a
> string is a valid filename?  If not, can someone refresh my memory;
> what is a valid filename in Unix?  I think it can be up to 255
> characters, but which characters are disallowed?

Not one to say if a pathname/filename is "valid" specifically, but there
is one to test if a file exists ('os.path.exists').

Filenames may contain any character except the slash (/, '\057\) and the
null character ('\000').  Name components in a full pathname to a file are
seperated by slashes (e.g. "/home/arcege/.login").  And in the language C,
strings are terminated by the null character; an embedded null character
would mean that most every program in UNIX would see a shortened filename.

A UNIX filename can be just about anything, UNIX won't really care.
Some utilities and scripting languages will have problems tho.  Python
will take "weird" filename as just another string and use it without
problems (except that Python can embedded null characters).

But in general, filenames can be kept to lower-case and upper-case
characters, numerals and punctuation.  You might want to look into the
fnmatch module.

:OT:
There was a recent thread here about the "disadvantages of UNIX not
evaluating filename extentions".  UNIX evaluates nothing - a filename can
be anything.  It is up to the application to handle the file.  There are
utilities to determine file type (for example, file(1)), having the
operating system determine this is detrimental to the power of UNIX.
:OT:

  -Arcege

-- 
+----------------------------------+-----------------------------------+
| Michael P. Reilly                | arcege@speakeasy.net              |