473,847 Members | 1,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 28867
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.spa m.com> wrote in message
news:c6******** **@atlantis.new s.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.tx t" will be
read as "C:<CTRL-F>ile.txt", which is not valid. However both
"c:\\file.t xt" 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**********@a tlantis.news.tp i.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****@swamiso ft.com> wrote:
"Maciej Sobczak" <no*****@no.spa m.com> wrote in message
news:c6******** **@atlantis.new s.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
6864
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 -------------------------------------- inPath = Me.txtPath.Text 'CHECK PATH ----------------------------
2
3519
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 idea how to check a creation date it would be appreciated. Thanks dave def delete_old_files (t:\dm\~\users)
2
5367
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 Database Public Function adhFileExists(strName As String) As Boolean On Error Resume Next
4
5317
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 legal" error. I attempted uninstalling it and reinstalling to no avail... I have also upgraded to Visual Studio.net 2003 and am receiving the same error message. Additionally, I tested adding an image at design time to an ASP.NET Web Application...
2
10798
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 further information on this error - does anyone know what the problem and resolution might be? thanks
2
3532
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. We noticed that the last line in the following rs("vpath") value lists the path up until the first blank of either a folder or file name and cuts off the rest of the path's text name. Does anyone know how to have the vpath list the entire path...
2
12536
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 an exception like System.ArgumentException: Empty path name is not legal. at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options,...
1
4413
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 they looked perfectly fine and the corresponding directories still exist and looked ok I also check the project references what else can the message mean?
3
6992
by: pantagruel | last post by:
Hi, the following: FileSystemWatcher watcher = new FileSystemWatcher(); RenderingQ = ConfigurationSettings.AppSettings; //RenderingQ = "C:\\RenditionServerQ\\InjectorQ\\OnDemandQ"; watcher.Path = RenderingQ; raises the following error:
0
9892
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
10991
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10653
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10718
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10347
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7061
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5725
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5915
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3168
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.