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





On Tue, Apr 28, 2009 at 3:43 PM, David <[email protected] <mailto:[email protected]>> wrote:

    I am getting information from .txt files and posting them in fields
    on a web site. I need to break up single strings so they are around
    80 characters then a new line because when I enter the info to the
    form on the website it has fields and it errors out with such a long
    string.

    here is a sample of the code;

    #!/usr/bin/python
    import subprocess
    import os

    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 = len(USE)
    print L
    print USE

    L returns 1337

    Here is what USE returns;
    http://linuxcrazy.pastebin.com/m2239816f

    thanks
    -david
-- Powered by Gentoo GNU/Linux
    http://linuxcrazy.com
    _______________________________________________
    Tutor maillist  -  [email protected] <mailto:[email protected]>
    http://mail.python.org/mailman/listinfo/tutor


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']

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')
    fname = 'usetest.txt'
    fobj = open(fname, 'w')
    fobj.write(Lines)
    fobj.close

subopen()

Here is the output;
http://linuxcrazy.pastebin.com/m66105e3

--
Powered by Gentoo GNU/Linux
http://linuxcrazy.com
_______________________________________________
Tutor maillist  -  [email protected]
http://mail.python.org/mailman/listinfo/tutor

Reply via email to