Preventing 'bad' filenames from raising errors in os.path

P

python

Bad file names, i.e. filenames the OS considers illegal, will cause
functions in the os.path module to raise an error.

Example:

import os.path
print os.path.getsize( 'c:/pytest/*.py' )

On Windows XP using Python 2.5.2 I get the following traceback:

Traceback (most recent call last):
File "<string>", line 74, in run_nodebug
File "<Module1>", line 3, in <module>
File "C:\Python\lib\ntpath.py", line 228, in getsize
return os.stat(filename).st_size
WindowsError: [Error 123] The filename, directory name, or volume label
syntax is incorrect: 'c:/pytest/*.py'

Since there are many places a user can enter a path name (interactively,
via config files, etc) in most applications, is there an os sensitive
function that can be used to detect bad file names?

As a matter of best practice, how do you wrap your use of file and path
names to prevent unexpected failures? (There has to be a better
alternative than try/except blocks around each use of an os.path
function in one's code?)

Thanks,
Malcolm
 
M

Matimus

Bad file names, i.e. filenames the OS considers illegal, will cause
functions in the os.path module to raise an error.

Example:

import os.path
print os.path.getsize( 'c:/pytest/*.py' )

On Windows XP using Python 2.5.2 I get the following traceback:

Traceback (most recent call last):
File "<string>", line 74, in run_nodebug
File "<Module1>", line 3, in <module>
File "C:\Python\lib\ntpath.py", line 228, in getsize
return os.stat(filename).st_size
WindowsError: [Error 123] The filename, directory name, or volume label
syntax is incorrect: 'c:/pytest/*.py'

Since there are many places a user can enter a path name (interactively,
via config files, etc) in most applications, is there an os sensitive
function that can be used to detect bad file names?

As a matter of best practice, how do you wrap your use of file and path
names to prevent unexpected failures? (There has to be a better
alternative than try/except blocks around each use of an os.path
function in one's code?)

Thanks,
Malcolm

What do you find troubling about try/except?

Compare:

import os.path

path = raw_input("enter a path:")
if isvalidpath(path): # this is a made up name
print os.path.getsize(path)
else:
# Do something else

To:

import os.path

path = raw_input("enter a path:")
try:
print os.path.getsize(path)
except WindowsError:
# Do something else

Now, I can understand if you don't like the "WindowsError" as that is
obviously platform specific. The try/except pattern however is the way
errors are handled in python and the best and most appropriate way to
deal with it. The above example just shows that at the very least
there isn't a significant difference in code size between the two
methods.

I don't know what the equivalent error is called in *nix but assume it
is PosixError (it isn't), then it would just be written this way:

import os.path

path = raw_input("enter a path:")
try:
print os.path.getsize(path)
except (WindowsError, PosixError):
# Do something else


You don't _always_ need to wrap os.path functions with try/except. You
only need to wrap where there is reason to expect that the input might
be prone to error. In those cases the alternative is what? wrapping it
in a if/else instead? How is that better?

Matt
 
M

Martin v. Löwis

Now, I can understand if you don't like the "WindowsError" as that is
obviously platform specific. The try/except pattern however is the way
errors are handled in python and the best and most appropriate way to
deal with it. The above example just shows that at the very least
there isn't a significant difference in code size between the two
methods.

I don't know what the equivalent error is called in *nix but assume it
is PosixError (it isn't)

The common base class to use here is actually OSError. WindowsError is
a subclass thereof.

Regards,
Martin
 
J

John Nagle

Bad file names, i.e. filenames the OS considers illegal, will cause
functions in the os.path module to raise an error.

Example:

import os.path
print os.path.getsize( 'c:/pytest/*.py' )

The issue, I think, is there's no function that checks
syntax of a path name without checking with the underlying OS
to see if the file exists.

If, say, you're writing a GUI tool for setting up some configuration,
you'd want to do input validation on fields without actually
accessing the files.

John Nagle
 
P

python

Matimus and John,

Thank you both for your feedback.

Matimus: I agree with your analysis. I blame lack of caffeine for my
original post :)

Regards,
Malcolm
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top