write a .txt file

MRAB python at mrabarnett.plus.com
Tue Jul 13 15:40:16 EDT 2010


Jia Hu wrote:
> 
> 
> On Tue, Jul 13, 2010 at 3:17 AM, Cameron Simpson <cs at zip.com.au 
> <mailto:cs at zip.com.au>> wrote:
> 
>     On 13Jul2010 02:46, Jia Hu <hujia06 at gmail.com
>     <mailto:hujia06 at gmail.com>> wrote:
>     | 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
> 
>     You should not need the .close().
>      
> 
> Thank you.
>  
> 
>     | desLrr = subprocess.Popen('ls -a >> final.txt', shell=True)  #
>     change to "ls
>     | -a"
> 
>     You're still not waiting for the Popen subprocess to finish before
>     continuing.
> 
>     | fileName=open ('final.txt', 'a')
> 
>     You shouldn't need the open() if you skip the .close()
> 
>     | fileName.seek(0,2)
> 
>     If you _do_ do an open(..., "a") then you don't need the seek - append
>     mode will write at the end for you.
> 
>     | 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.
> 
>     First: please don't top post. Post below the relevant points as I do
>     above, like a conversation. That way it is clear exactly what each
>     remark is for.
> 
>  
> 
>     OK, it is a good suggestion. By the way, do you add the pipe "|" or
> 
> they are automatically generated ?
> 
> 
>     Second: "flush" is a general idea used with buffers.
> 
>     When you .write() to a file the data does not normally go directly to
>     disc.  This is because a real write to the disc is expensive in time
>     (the CPU is much much faster than a hard drive).
> 
>     Also, a OS-level "write", which transfers the data from your program
>     into
>     the OS's buffers (where it queues, for eventual writing to the disc)
>     is _also_ a comparitively expensive operation. So when you .write()
>     in python (or C using stdio, and in many other languages) the data is
>     normally kept in a memory buffer inside your program. Only when that
>     buffer is filled is an OS-level "write" done. In this way, OS calls are
>     few. This is a performance gain.
> 
>     So, in your program, when you .write() your first line of numbers
>     the data has not been handed to the OS at all, and therefore the
>     "final.txt" file has not seen it. A .flush() call is an explicit request
>     to empty the buffer, making an OS-level "write" immediately. In your
>     program, this is necessary to get the data to "final.txt" before the
>     "echo" or "ls" commands are run.
> 
>     If you .close() a file, that also empties the buffer. But it also closes
>     the file! SO you need to re-open it. For your purposes, a .flush() is
>     enough and leaves the file open for you to continue using it later.
> 
>     Regarding the .seek(): after the "echo" or "ls" command has run
>     the file  has grown. Python's "file" object does not know the file has
>     grown because it was not involved. So a .seek(0 is needed to position
>     Python's .write() location to the end of the file instead of where it
>     though things were.
> 
>     Um. Because you have opened the file in 'a' mode you should not need the
>     .seek() - an "append" mode file will always write to the end of the
>     file.
> 
>     So: your first problem only requires a .flush(), to empty the buffer
>     before "echo" or "ls" runs. However, you still have not waited for the
>     "echo" or "ls" to finish - Popen kicks the command off, but it will run
>     at the same time as your program! Call:
> 
>      desLrr.wait()
> 
> I initially thought only desLrr.wait() is needed even if without 
> fileName.flush() , that is
> because the first line of number will transfer to the memory buffer and 
> then "echo, ls" is
> also kept in the memory buffer and then wait() to make them finish. But 
> when I actually did this, 
> I find this method is not very correct. The first few words generated 
> by "ls" are missing.
> When I add  "fileName.flush()" again and got a correct output.
>  
> Should I always write to the file at one time and then run "flush" or 
> "close()" before the next
> write to the file (e.g. "echo" or the second line of number) ?
>  
Each process will have its own buffers, so the output from "echo" won't
be going into the same buffer as that of your Python script.

Whether you need to close the file depends on whether the open file can
be shared. When I tried it on Windows XP without closing the file I got
an error message saying that it ("echo") couldn't write to the file
because it was open in another process (the Python script).

If the open file can be shared then you just need to ensure that the
script's output is flushed to disk; if the open file can't be shared
then you need to close the file (any output that's still in the buffer
will be flushed automatically when the file is closed).



More information about the Python-list mailing list