write a .txt file
Jia Hu
hujia06 at gmail.com
Tue Jul 13 02:46:14 EDT 2010
Hi:
Do you mean the following code?
#!/usr/bin/python
# OS: Ubuntu
import subprocess
fileName = open ('final.txt', 'a')
fileName.write ('%s %s %s \n' % (12,25,9))
fileName.flush() # add
fileName.close() # add
desLrr = subprocess.Popen('ls -a >> final.txt', shell=True) # change to "ls
-a"
fileName=open ('final.txt', 'a')
fileName.seek(0,2)
fileName.write ('%s %s %s \n' % (85,25,12))
fileName.close()
I run that, the result showed the list of file is located after the number
list 85 25 12
Is that possible that I put the fileName.flush() in a wrong position ?
I am new to Python and do not quite understand the "flush" concept.
Thank you.
Jia
> On Tue, Jul 13, 2010 at 2:16 AM, Cameron Simpson <cs at zip.com.au> wrote:
>
>> On 12Jul2010 21:28, Jia Hu <hujia06 at gmail.com> wrote:
>> | 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))
>>
>> String still in Python's buffer, not yet in the file. Add:
>>
>> fileName.flush()
>>
>> to ensure data in file before running echo.
>>
>> | desLrr = subprocess.Popen('echo "hello" >> final.txt ', shell=True)
>>
>> Command dispatched, but not yet waited for. So your Python program
>> proceeds
>> and writes to the file _before_ the echo command gets to run.
>>
>> Wait for desLrr to complete before proceeding.
>>
>> | fileName.seek(0,2)
>> | fileName.write ('%s %s %s \n' % (85,25,12))
>> | fileName.close()
>>
>> And the rest is ok.
>>
>> Cheers,
>> --
>> Cameron Simpson <cs at zip.com.au> DoD#743
>> http://www.cskk.ezoshosting.com/cs/
>>
>> We tend to overestimate the short-term impact of technological change and
>> underestimate its long-term impact. - Amara's Law
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100713/8cdfa72f/attachment-0001.html>
More information about the Python-list
mailing list