David wrote: > vince spicer wrote: >> first, grabbing output from an external command try: >> >> import commands >> >> USE = commands.getoutput('grep USE /tmp/comprookie2000/emege_info.txt >> |head -n1|cut -d\\"-f2') >> >> then you can wrap strings, >> >> import textwrap >> >> Lines = textwrap.wrap(USE, 80) # return a list >> >> so in short: >> >> import commands, textwrap >> data = textwrap.wrap(commands.getoutput('my command'), 80) >> >> >> >> Vince > Thanks Vince, > I could not get command to work, but I did not try very hard; > ["cut: the delimiter must be a single character Try `cut --help' for > more", 'information. head: write error: Broken pipe']
Ah, I see. This error is most likely due to the typo (missing space before -f2). > > But textwrap did the trick, here is what I came up with; > > #!/usr/bin/python > > import subprocess > import os > import textwrap > import string > > def subopen(): > u_e = subprocess.Popen( > 'grep USE /tmp/comprookie2000/emerge_info.txt |head -n1|cut > -d\\" -f2', > shell=True, stdout=subprocess.PIPE,) > os.waitpid(u_e.pid, 0) > USE = u_e.stdout.read().strip() > L = textwrap.wrap(USE, 80) # return a list > Lines = string.join(L, '\n') Just one more comment, string.join is deprecated, yet join is a method of str objects. So ... Lines = '\n'.join(L) ... or use textwrap.fill which returns a string with the newlines already in place ... Lines = textwrap.fill(USE, 80) HTH, Marty > fname = 'usetest.txt' > fobj = open(fname, 'w') > fobj.write(Lines) > fobj.close > > subopen() > > Here is the output; > http://linuxcrazy.pastebin.com/m66105e3 > _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor