Writing linux term color codes to a file

stasZ stasZ at linuxisbeter.nl
Wed Sep 18 08:41:51 EDT 2002


On Wed, 18 Sep 2002 03:09:37 +0200, Andrei Kulakov wrote:

> Hello,
> 
> I ran into a bit of a problem here.. I'm making a small app that
> visually creates bash prompts (just for fun of it), and at one point it
> has to write the prompt to .profile, optionally with linux terminal
> color codes, like \[044;\]m or something like that (you know what I'm
> talking about). They don't come out right *in* the file. I tested the
> simple case where I'd have a few such codes in a string in python prompt
> and I'd look at it and it looks fine, but as soon as I write it out to a
> file and look at the file, it's "cooked".
> 
> I hope you know what I mean, if anyone wants I can post direct pastes.
> 
> Looks like write() does some sort of processing on the text; is there
> any way to turn that off?

>>>prompt = r'\[044;\]'
>>> prompt
'\\[044;\\]'

>>> f = open('/home/foo/prompt,'w')
>>> f.write(prompt)
>>> f.close()

$ more prompt
\[044;\]

I think your problem is solved if you use a raw string (r'spam').
With a raw string the backslash is just a backslash.

Stas Z

-- 
A computer scientist is someone who, when told to 'Go to Hell', 
sees the 'go to', rather than the destination, as harmful.



More information about the Python-list mailing list