Writing linux term color codes to a file

Andrei Kulakov ak at silmarill.org
Thu Sep 19 00:10:58 EDT 2002


On Wed, 18 Sep 2002 14:41:51 +0200, stasZ <stasZ at linuxisbeter.nl>
wrote:

>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.

Thanks, I think that's it. I'll try it tonight. You've been doubly
helpful - that' sthe funniest sig I've seen this month. :P
>
>Stas Z




More information about the Python-list mailing list