473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
9 28542
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
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
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


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
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
[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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: build | last post by:
G'day All, I'm using code below to check a path selected by a user. Can anyone pick holes in it please? The more critical you are the better. tia build 'GET SOURCE PATH...
2
by: David Fickbohm | last post by:
People, I am trying to determine the creation date of files in a folder. I am using the following code to find the folder and confirm that files exist in the folder. If someone could give me an...
2
by: David | last post by:
I'm using following code for checking a file existence. I's working fine for given folder. Is there a way to check a file exitance in subfolders? Thanks in advance, David Option Compare...
4
by: Chas Stapleton | last post by:
I have recently uninstalled VB5 and installed vb.net 2002. In working on an application, I attempted to add a picturebox and add an image... I immediately received an "empty path name is not...
2
by: Richard Bysouth | last post by:
Hi When attempting to view inherited forms in design mode I have been getting the message "The path is not of a legal form" and am unable to view the designer. I can't seem to find any...
2
by: zz12 | last post by:
Hello. We have a SearchResults.asp page that returns a list of asp pages that are associated with the user's search parameters that they type in in a search page called something like Search.asp. ...
2
by: ranjith konnoju | last post by:
hi, i am getting an exception while populating the data in the datagrid. am getting the values into datatable and binding it to the datagrid but it is not displaying the data and giving...
1
by: GS | last post by:
I got this warning for an vb application when I opened it in vb .net studio 2005 express Warning 1 The path is not of a legal form. 0 0 I thought it was project reference path. I checked and...
3
by: pantagruel | last post by:
Hi, the following: FileSystemWatcher watcher = new FileSystemWatcher(); RenderingQ = ConfigurationSettings.AppSettings; //RenderingQ = "C:\\RenditionServerQ\\InjectorQ\\OnDemandQ";...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.