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

A.T.Hofkamp a.t.hofkamp at tue.nl
Fri May 15 12:45:34 CEST 2009


MK wrote:
> Ok. I explain it once more. Its not about reading the console output of
> the command. Its that i want to put a empty file in my subdirectories
> so that the name of the file is a message or url or something else.
> And if you want an empty file you can do that with 
> "cat > IAmAnEmptyFileWithOnlyAName"
> under linux on the CLI.

'touch filename' is easier when you know the file does not yet exist.

Alternatively, you can do 'cat > filename < /dev/null'

> But the problem or advantage of is that you can put some lines in
> the text file if you want. And if youre ready youm must
> terminate the console/terminal with CTRL+D so that cat knows
> that the input is finished.

CTL+D means EOF. In a program you express that by closing the file handle.

fp = open('xyz', 'w')
fp.close()

for creating an empty file, thus

fp = subprocess. ....  #get file handle for writing data to the child
fp.close()

for stating that you have no data for the child process.

> I have no lines i want to input in the file. So i must only send
> and CTRL+D to cat again that it terminates.
> 
> Actually a very simple way i thought. But maybe not.

It is simple indeed :)
It is not Python specific, but defined by the underlying OS. That is why you 
don't find this information in Python documentation.


Albert




More information about the Tutor mailing list