uu.py - get result of uuencode() in a string

Thomas Wouters thomas at xs4all.nl
Wed Jul 21 10:17:05 EDT 1999


On Wed, Jul 21, 1999 at 03:40:17PM +0200, Richard van de Stadt wrote:

> Is it possible to get the output of the uuencode() function
> into a string, instead of a file or stdout?

You can give uu.encode() File objects, so you can also give them StringIO
objects.

>>> import StringIO
>>> import uu
>>> import operator
>>> infile = open("/etc/hosts.allow",'r') 
>>> outfile = StringIO.StringIO()
>>> uu.encode(infile, outfile)
>>> outfile.buflist  
['begin 666 -\012',
'M(PHC(&AO<W1S+F%L;&]W"51H:7, at 9FEL92!D97-C<FEB97,@=&AE(&YA;65S\012',
'M(&]F(\'1H92!H;W-T<R!W:&EC:"!A<F4*(PD)86QL;W=E9"!T;R!U<V4@=&AE\012',
'M(&QO8V%L($E.150@<V5R=FEC97,L(&%S(&1E8VED960*(PD)8GD@=&AE("<O\012',
':=7-R+W-B:6XO=&-P9"<@<V5R=F5R+ at HC"@H \012', ' \012end\012']

>>> print reduce(operator.add, outfile.buflist)
begin 666 -
M(PHC(&AO<W1S+F%L;&]W"51H:7, at 9FEL92!D97-C<FEB97,@=&AE(&YA;65S
M(&]F('1H92!H;W-T<R!W:&EC:"!A<F4*(PD)86QL;W=E9"!T;R!U<V4@=&AE
M(&QO8V%L($E.150@<V5R=FEC97,L(&%S(&1E8VED960*(PD)8GD@=&AE("<O
:=7-R+W-B:6XO=&-P9"<@<V5R=F5R+ at HC"@H 
 
end

>>> 

StringIO objects are simple string objects that behave like filehandles
(they support the same functions) but store their data in strings.
My apologies for the ugly print reduce(operator.add) etc, but i haven't had
much more than 5 hours sleep in the last three days. work work work ;-P

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list