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

Sander Sweers sander.sweers at gmail.com
Fri May 15 02:54:36 CEST 2009


Forwarding to list because of mail delivery error...

2009/5/15 Sander Sweers <sander.sweers at gmail.com>:
> 2009/5/14 MK <lopoff at gmx.net>:
>> i am using this code to send an "cat > ThisIsMyUrl" with popen.
>
> First, I am not sure if you understand what "cat > ThisIsMyUrl"
> actually does... It waits for data on *stdin* and pipes it to
> ThisIsMyUrl. It does not provide any data to read! What is it that you
> want to accomplish?
>
> Anyway, why use cat and not use open() and write the data into it?
>  file = open(working_dir + "/" + subdir + "www.thisismyurl.com", "w")
>  file.write(your_data_string)
>  file.close()
>
>> Of cos cat now waits for the CTRL+D command.
>
> Well yes and no, cat is waiting for data on stdin and you do not seem
> to give any data to it.
>
>> How can i send this command ?
>
> CTR-D/EOF normally comes from the data you are piping into cat. For
> example the below, it pipes the string "test" to the stdin from cat
> which then redirects to a file test.txt.
>  echo "test" | cat > test.txt
>
>> def console_command(cmd):
>>        print cmd
>>        console = os.popen(cmd,"r")
>>        output = console.read()
>>        console.close()
>>        return output
>>
>> command="cat > " + working_dir + "/" + subdir + "www.thisismyurl.com"
>> console_command(command)
>
> You should really look at the subprocess module [1].
>
> Greets
> Sander
>
> [1] http://docs.python.org/library/subprocess.html
>


More information about the Tutor mailing list