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

Gabriel Cooper gabriel.cooper at mediapulse.com
Tue Apr 20 10:16:51 EDT 2004



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.




More information about the Python-list mailing list