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

M

Maciej Sobczak

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?
 
G

Gerrit

Maciej said:
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.
 
L

Larry Bates

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

Gabriel Cooper

Maciej said:
[...]
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.
 
P

Peter Hansen

Maciej said:
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
 
B

Bill Rubenstein

What is the problem with just creating the file and handling the error case with an except
clause?

Bill
 
J

Jorgen Grahn

[top-posting fixed]

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
 
P

Peter Hansen

Jorgen said:
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
 
R

Roger Binns

Heather said:
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
 

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

Forum statistics

Threads
474,189
Messages
2,571,015
Members
47,616
Latest member
gijoji4272

Latest Threads

Top