472,142 Members | 1,211 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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
Jun 27 '08 #1
4 3138
On May 2, 9:40 am, pyt...@bdurham.com wrote:
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
Jun 27 '08 #2
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
Jun 27 '08 #3
py****@bdurham.com wrote:
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
Jun 27 '08 #4
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
Jun 27 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by hokiegal99 | last post: by
3 posts views Thread by Kevin Ollivier | last post: by
2 posts views Thread by rbutch | last post: by
1 post views Thread by Klaus Jensen | last post: by
3 posts views Thread by Torsten Bronger | last post: by
2 posts views Thread by =?Utf-8?B?SmVzcGVyLCBEZW5tYXJr?= | last post: by

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.