file path & windows cgi

Carsten Gaebler clpy at snakefarm.org
Thu Sep 27 16:35:40 EDT 2001


Scott Griffitts wrote:
> 
> Trying to read a file, this script works for me:
> 
> z = open('I:\\test\\test.txt')
> for x in z.readlines():
>     b = "%s%s" % (b,x)
> z.close()
> print b
> 
> But if I try and get the path to the file via an html form:
> 
> import cgi
> y = cgi.FieldStorage()
> z = open(y['getter'].value)
> for x in z.readlines():
>     b = "%s%s" % (b,x)
> z.close()
> print b
> 
> I get:
> 
> Traceback (most recent call last):
>   File "c:\inetpub\wwwroot\python\brow2.py", line 5, in ?
>     z = open(y['getter'].value)
> IOError: [Errno 22] Invalid argument: 'I:\\test\\test.txt'
> 
> Does anyone know what I'm missing here?  Thanks.

I think the reason is that in the first example you give the
filename as a string literal and thus have to escape the
backslash. In the second example you get the filename from an HTML
form. Most probably you typed it in with the double backslash.
Don't do that that because in this case the string is not parsed
by the Python interpreter, so the backslash has no special
meaning.

cg.



More information about the Python-list mailing list