Preventing 'bad' filenames from raising errors in os.path
python at bdurham.com
python at bdurham.com
Fri May 2 12:40:21 EDT 2008
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
More information about the Python-list
mailing list