python - Sanitizing user input path names -


using python 2.6 on windows have function needs accept path name arg. i'm running issues when specific paths passed.

c:\users\bob\something.png    #this handled no prob. c:\users\bob\nothing.png      #this generates windowserror c:\users\bob\test.png         #this generates windowserror 

what i'm gathering \n in "nothing" path being interpreted new line, , \t in "test" path being interpreted tab.

if print out path names, that's appears happening.

print os.path.abspath("c:\users\bob\nothing.png") c:\users\bob othing.png 

same 'test' path, except tab instead of new line.

the thing i've come far check see if \n or \t in path name, , handle accordingly, there must assuredly better way.

if '\n' in path_name:     #escape slash, move on 

what better way be?

print os.path.abspath(r"c:\users\bob\nothing.png") 

may looking ...

although user input should automatically escaping slash ...

>>> = raw_input("enter path:") enter path:c:\www\a\nothing.jpg >>> 'c:\\www\\a\\nothing.jpg' 

as can see example user input indeed escaped


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -