write a .txt file

Jia Hu hujia06 at gmail.com
Tue Jul 13 01:51:49 EDT 2010


If I change "'echo "hello">> final.txt" to "ls -a >> final.txt" and add
fileName.close() before subprocess.

The result is still like:
12 25 9
85 25 12
.... # list of files
.....

How can I put the list before the number list 85 25 12?

Thank you.


On Tue, Jul 13, 2010 at 12:47 AM, Jia Hu <hujia06 at gmail.com> wrote:

> Thank you. I will try that.
>
> The code was simplified to show my problem. I do not use fileName as
> a variable in my code. Thanks for your reminding.
>
>   On Mon, Jul 12, 2010 at 9:58 PM, MRAB <python at mrabarnett.plus.com>wrote:
>
>>  Jia Hu wrote:
>>
>>> Hello:
>>>  I have a problem about how to generate a specific.txt file. I use the
>>> following code:
>>>  #!/usr/bin/python
>>> # OS: Ubuntu
>>> import subprocess
>>> fileName = open ('final.txt', 'a')
>>> fileName.write ('%s %s %s \n' % (12,25,9))
>>> desLrr = subprocess.Popen('echo "hello" >> final.txt ', shell=True)
>>>
>>> fileName.seek(0,2)
>>> fileName.write ('%s %s %s \n' % (85,25,12))
>>> fileName.close()
>>>  The above code generates the following result:
>>> 12 25 9
>>> 85 25 12
>>> hello
>>>  What I want is:
>>> 12 25 9
>>> hello
>>> 85 25 12
>>>  Could someone provies some suggestions about this problem?
>>>  Thank you.
>>>
>>> When "echo" is called the file is still open, so the might not be able
>> to write to it. Try closing the file before starting the subprocess and
>> then reopen it afterwards.
>>
>> By the way, it slightly confusing that you call the variable "fileName",
>> but it doesn't contain the name of a file! :-)
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100713/bb919324/attachment.html>


More information about the Python-list mailing list