By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,018 Members | 870 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,018 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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" <no*****@no.spam.com> wrote in message
news:c6**********@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.

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/

Jul 18 '05 #2

P: n/a
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
Jul 18 '05 #3

P: n/a
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/

Jul 18 '05 #4

P: n/a


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.

Jul 18 '05 #5

P: n/a
What is the problem with just creating the file and handling the error case with an except
clause?

Bill

In article <c6**********@atlantis.news.tpi.pl>, no*****@no.spam.com 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?

Jul 18 '05 #6

P: n/a
[top-posting fixed]

On Tue, 20 Apr 2004 09:07:51 -0500, Larry Bates <lb****@swamisoft.com> wrote:
"Maciej Sobczak" <no*****@no.spam.com> wrote in message
news:c6**********@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?''
Jul 18 '05 #7

P: n/a
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
Jul 18 '05 #8

P: n/a
On Sat, 24 Apr 2004 08:52:31 -0400,
Peter Hansen <pe***@engcorp.com> 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
Jul 18 '05 #9

P: n/a
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
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.