Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > How to check if a path *could* be a legal path?

Reply
Thread Tools

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

 
 
Maciej Sobczak
Guest
Posts: n/a
 
      04-20-2004
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/

 
Reply With Quote
 
 
 
 
Gerrit
Guest
Posts: n/a
 
      04-20-2004
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/

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      04-20-2004
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" <(E-Mail Removed)> wrote in message
news:c62ums$b3b$(E-Mail Removed)...
> 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/
>



 
Reply With Quote
 
Gabriel Cooper
Guest
Posts: n/a
 
      04-20-2004


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.

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-20-2004
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
 
Reply With Quote
 
Bill Rubenstein
Guest
Posts: n/a
 
      04-20-2004
What is the problem with just creating the file and handling the error case with an except
clause?

Bill

In article <c62ums$b3b$(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
>
>

 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      04-24-2004
[top-posting fixed]

On Tue, 20 Apr 2004 09:07:51 -0500, Larry Bates <(E-Mail Removed)> wrote:
> "Maciej Sobczak" <(E-Mail Removed)> wrote in message
> news:c62ums$b3b$(E-Mail Removed)...
>> 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?''
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-24-2004
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
 
Reply With Quote
 
Heather Coppersmith
Guest
Posts: n/a
 
      04-24-2004
On Sat, 24 Apr 2004 08:52:31 -0400,
Peter Hansen <(E-Mail Removed)> 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
 
Reply With Quote
 
Roger Binns
Guest
Posts: n/a
 
      04-24-2004
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
check path.exists() with a "converted" path Alessandro Python 5 09-27-2010 09:51 AM
regexp for legal JNDI name (covering check for 2 space sequence) davidmichaelkarr@gmail.com Java 11 09-26-2005 05:29 PM
Server.MapPath() croaking with "failed to map path", but path is legal awbacker@gmail.com ASP .Net 0 05-12-2005 10:50 PM
Bizzare: Invalid path to Access mdb, path is good Keith-Earl ASP .Net 1 05-03-2004 03:25 PM



Advertisments