[Tutor] string conversion according to the terminal

Alan Gauld alan.gauld at btinternet.com
Fri Aug 13 10:06:27 CEST 2010


"ANKUR AGGARWAL" <coolankur2006 at gmail.com> wrote
> Hey- suppose we have a file name-"my file number"
> if we want to execute it into the terminal it would be like - my\ 
> file\
> number

It depends what  you are trying to do.

> so wondering is there any one in the python that change the enter 
> string
> into the terminal string one- like if user enter the file name with
> path- "my file number". i want to automatically convert it into "my\ 
> file\ number"

If you use raw_input to capture the filename then the escaping will be 
done
for you when you come to use it.

>>> s = raw_input('?')
?name with spaces
>>> s
'name with spaces'
>>> f = open(s,'w')
>>> f.write('hello world\n')
>>> f.close()
>>> f = open(s)
>>> f.read()
'hello world\n'
>>> f.close()
>>> ^Z


C:\Documents and Settings\Alan Gauld>dir n*
 Volume in drive C is SYSTEM
 Volume Serial Number is 7438-BB1D

 Directory of C:\Documents and Settings\Alan Gauld

13/08/2010  09:03                13 name with spaces


So are you sure you need to do this?

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list