[Tutor] Python popen command using cat > textfile .... how to terminate

Walker Hale IV walker.hale.iv at gmail.com
Sun May 17 18:33:40 CEST 2009


On Fri, May 15, 2009 at 12:46 PM, Noufal Ibrahim <noufal at nibrahim.net.in> wrote:
> MK wrote:
>>
>> Hi there,
>>
>> i am using this code to send an "cat > ThisIsMyUrl" with popen.
>> Of cos cat now waits for the CTRL+D command. How can i send this command ?
>
> Wouldn't it be better if you directly opened the "ThisIsMyUrl" file and
> wrote the text into it rather than rely on shelling out for this?
>
> [..]
>
> --
> ~noufal
> http://nibrahim.net.in/

Agreed. Something like this perhaps?

import sys
def getFileFromUser():
    f = open('ThisIsMyUrl', 'w')
    while True:
        s = sys.stdin.readline()
        if len(s) == 0:
            break # empty string denotes EOF
        f.write(s)
    f.close()

Seems to work on my Mac.

-- 
Walker Hale <walker.hale.iv at gmail.com>


More information about the Tutor mailing list