Re: write a .txt file

2010-07-13 Thread MRAB

Jia Hu wrote:



On Tue, Jul 13, 2010 at 3:17 AM, Cameron Simpson > wrote:


On 13Jul2010 02:46, Jia Hu mailto:huji...@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).
--
http://mail.python.org/mai

Re: write a .txt file

2010-07-13 Thread Jia Hu
On Tue, Jul 13, 2010 at 3:17 AM, Cameron Simpson  wrote:

> On 13Jul2010 02:46, Jia Hu  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) ?

Thank you.



>

to wait for the "echo" or "ls" to finish before continuing!
>

> Cheers,
> --
> Cameron Simpson  DoD#743
> http://www.cskk.ezoshosting.com/cs/
>
> Quick Guide to Resources to break crypto systems of given strength:
> Difficulty Resources required
>  2**0  Pencil and paper
>  2**8  Pencil and paper and a lot of patience
>  2**16 Warez d00d with a Commodore 64
>  2**32 Amateur with an average PC
>  2**40 Smart amateur with a good PC
>  2**56 Network of workstations, custom-built hardware
>  2**64 Thousands of PCs working together for several years
>  2**80 NSA, Microsoft, the Illuminati
>  2**128Cubic kilometers of sci-fi nanotech
>  2**160Dyson spheres
> Eddy L O Jansson and Matthew Skala, from:
> http://hem.passagen.se/eddy1/reveng/cp4/cp4break.html
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a .txt file

2010-07-13 Thread Cameron Simpson
On 13Jul2010 02:46, Jia Hu  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().

| 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.

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()

to wait for the "echo" or "ls" to finish before continuing!

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Quick Guide to Resources to break crypto systems of given strength:
Difficulty Resources required
 2**0  Pencil and paper
 2**8  Pencil and paper and a lot of patience
 2**16 Warez d00d with a Commodore 64
 2**32 Amateur with an average PC
 2**40 Smart amateur with a good PC
 2**56 Network of workstations, custom-built hardware
 2**64 Thousands of PCs working together for several years
 2**80 NSA, Microsoft, the Illuminati
 2**128Cubic kilometers of sci-fi nanotech
 2**160Dyson spheres
Eddy L O Jansson and Matthew Skala, from:
http://hem.passagen.se/eddy1/reveng/cp4/cp4break.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a .txt file

2010-07-12 Thread Jia Hu
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  wrote:
>
>> On 12Jul2010 21:28, Jia Hu  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  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
>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a .txt file

2010-07-12 Thread Cameron Simpson
On 12Jul2010 21:28, Jia Hu  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  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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a .txt file

2010-07-12 Thread Jia Hu
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  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 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
>>
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: write a .txt file

2010-07-12 Thread MRAB

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


write a .txt file

2010-07-12 Thread Jia Hu
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.
-- 
http://mail.python.org/mailman/listinfo/python-list