469,293 Members | 1,364 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

write eof without closing

hello

can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

grts,
ruben
Aug 19 '06 #1
14 15081
In <ec**********@netlx020.civ.utwente.nl>, cage wrote:
can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something
What do you expect this to to? Writing a byte to the file and you don't
know which value this byte has?

Ciao,
Marc 'BlackJack' Rintsch
Aug 19 '06 #2
On 2006-08-19, cage <ca********************@gmail.comwrote:
can i write a eof to a file descriptor without closing it?
No. Not on Windows, OS-X, or Unix. There is no such thing as
"an eof".

On CP/M Ctrl-Z is used as EOF for text files.

--
Grant Edwards
gr****@visi.com

Aug 19 '06 #3
Marc 'BlackJack' Rintsch schreef:
In <ec**********@netlx020.civ.utwente.nl>, cage wrote:
>can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

What do you expect this to to? Writing a byte to the file and you don't
know which value this byte has?

Ciao,
Marc 'BlackJack' Rintsch
ok let me explain this a bit more...
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
Now I want a python script to provide the input, how do i do that? I now
use popen to be able to write to the program's stdin (p_stdin)
I noticed that when i do a p_stdin.close() it acts as a 'ctrl-d' in that
the program recognizes the signal to process the command, but then I
cannot use p_stdin anymore to do p_stdin.write(...)

grts,
ruben
Aug 19 '06 #4
>can i write a eof to a file descriptor without closing it?
>
No. Not on Windows, OS-X, or Unix. There is no such thing as
"an eof".

On CP/M Ctrl-Z is used as EOF for text files.
Common Dos/Window convention also uses ctrl+Z (0x1a) for EOF.

c:\copy con test.txt
hello
^Z
c:\>

*nix usually uses ctrl+D (0x04) as an EOF signal...and OS-X being
Unixish also uses the same.

bash$ cat test.txt
hello
^D
bash$

Don't know about Macs earlier than OS-X.

I don't know if there are problems (triggering an actual EOF and
closing the file) writing either of these ascii characters in
ascii/text mode (may vary between platforms?), but there are no
problems writing either of these characters in binary mode.

-tkc

Aug 19 '06 #5
cage wrote:
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
Now I want a python script to provide the input, how do i do that? I now
use popen to be able to write to the program's stdin (p_stdin)
I noticed that when i do a p_stdin.close() it acts as a 'ctrl-d' in that
the program recognizes the signal to process the command, but then I
cannot use p_stdin anymore to do p_stdin.write(...)
You might want to check the pty module in the Standard Library, and/or
Pexpect (http://pexpect.sf.net/)
Aug 19 '06 #6
cage schrieb:
Marc 'BlackJack' Rintsch schreef:
>In <ec**********@netlx020.civ.utwente.nl>, cage wrote:
>>can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

What do you expect this to to? Writing a byte to the file and you don't
know which value this byte has?

Ciao,
Marc 'BlackJack' Rintsch

ok let me explain this a bit more...
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
Now I want a python script to provide the input, how do i do that? I now
use popen to be able to write to the program's stdin (p_stdin)
I noticed that when i do a p_stdin.close() it acts as a 'ctrl-d' in that
the program recognizes the signal to process the command, but then I
cannot use p_stdin anymore to do p_stdin.write(...)
According to wikipedia (german version, but I bet you can get that info
using the english one, too) C-d sends EOT - end of transmission. Which
is ascii 0x04.

So I suggest you try writing

"\x04"

to the pipe. Maybe that works.

Diez
Aug 19 '06 #7
Writing the binary value for ^D into the stream will not do anything.
That value signals the shell to close the stream, as such it only has
significance when you're typing something into the shell.

To the OP: writing an EOF to a stream without closing it makes no
sense. EOF means just that--end of file. Once the program reaches an
EOF, it can't read any more values from the stream, so keeping it open
to write more stuff into it wouldn't even work.

Aug 19 '06 #8
Actually, nevermind. It appears that receiving an EOF from a stream
tells it when to stop 'reading', not necessarily that the stream is
closed. What a weird behavior.

Aug 19 '06 #9
cage wrote:
hello

can i write a eof to a file descriptor without closing it?
like:
fd.write(EOF)
or something

grts,
ruben
No but there is an EOF to the file anyway, even if it is open.

I recall under MS-DOS, you could create a file of size N without writing to
it (some INT21 or 9 ? call to modify the FAT) ... not really possible
anymore.

Philippe
Aug 19 '06 #10
cage wrote:
I want to use a program that has a 'pipe' mode, in which you can use
stdin to send commands to the program. I found out that, when in pipe
mode and you are using the keyboard as input source you can do Ctrl-D to
'signal' the program that you have finished typing your command. The
program parses and then performs the command, and it doesn't quit. It
quits after 'Quit\n' + Ctrl-D
have you tried *flushing* the output stream after each command?

</F>

Aug 20 '06 #11
On 2006-08-19, Tim Chase <py*********@tim.thechases.comwrote:
>>can i write a eof to a file descriptor without closing it?

No. Not on Windows, OS-X, or Unix. There is no such thing as
"an eof".

On CP/M Ctrl-Z is used as EOF for text files.

Common Dos/Window convention also uses ctrl+Z (0x1a) for EOF.

c:\copy con test.txt
hello
^Z
c:\>
IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.
*nix usually uses ctrl+D (0x04) as an EOF signal...and OS-X
being Unixish also uses the same.

bash$ cat test.txt
hello
^D
bash$
That's just the tty line-discipline layer of the tty driver.
When it sees Ctrl-D as the first thing on a "line", it closes
the file descriptor causing the client to see an EOF. That
feature in the line-discipline layer can be disabled or
configured to use any other character.

I suspect what the OP really needs to do is write a newline and
then flush the stream.

--
Grant Edwards grante Yow! I'm pretending that
at we're all watching PHIL
visi.com SILVERS instead of RICARDO
MONTALBAN!
Aug 21 '06 #12
Grant Edwards <gr****@visi.comwrote:
...
IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.
Easy to test, if you have Windows:
>>n='foo.txt'
s='ba\r\n'+chr(26)+'bo\r\r'
open(n,'wb').write(s)
ss=open(n).read()
ss
'ba\n'

As you see, in _text_ files on Windows a control-Z (char(26), AKA
'\x1a') does indeed represent "end of file" -- a convention going back
to CP/M (which lacked metadata to represent file length except in
multiples of 256 characters, if I recall correctly) and is still
followed by Windows (and by Python running on Windows).

Nevertheless I doubt it would help the original poster -- I think, like
/F and you, that a line-end and flush may be what he needs.
Alex
Aug 21 '06 #13
Alex Martelli wrote:
>IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.

Easy to test, if you have Windows:
>>>n='foo.txt'
s='ba\r\n'+chr(26)+'bo\r\r'
open(n,'wb').write(s)
ss=open(n).read()
ss
'ba\n'

As you see, in _text_ files on Windows a control-Z (char(26), AKA
'\x1a') does indeed represent "end of file"
your test doesn't match the OP's example, though, which used control-Z
to signal end of file when reading from the console:
>copy con test.txt
hello
^Z
1 file(s) copied.

that control-Z works in the same way as control-D on Unix, and no EOF
character is copied to the file:
>python -c "print repr(open('test.txt', 'rb').read())"
'hello\r\n'

</F>

Aug 21 '06 #14
On 2006-08-21, Alex Martelli <al***@mac.comwrote:
Grant Edwards <gr****@visi.comwrote:
...
>IIRC, ctrl-Z is not used _in_files_ to represent EOF. Only
when text is being entered at the console.

Easy to test, if you have Windows:
I might, but I won't admit it in public. :)
>>>n='foo.txt'
s='ba\r\n'+chr(26)+'bo\r\r'
open(n,'wb').write(s)
ss=open(n).read()
ss
'ba\n'

As you see, in _text_ files on Windows a control-Z (char(26), AKA
'\x1a') does indeed represent "end of file" -- a convention going back
to CP/M (which lacked metadata to represent file length except in
multiples of 256 characters, if I recall correctly)
That's correct.
and is still followed by Windows (and by Python running on
Windows).
Very interesting. I thought that windows had abandoned that. I
remember having problems under DOS/Windows caused by an old
text editor that put a ctrl-Z at the end of the file --
probably a result of the other programs reading the file in
binary mode and seeing the ctrl-Z.
Nevertheless I doubt it would help the original poster -- I
think, like /F and you, that a line-end and flush may be what
he needs.
--
Grant Edwards grante Yow! Am I elected yet?
at
visi.com
Aug 21 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Dennis M. Marks | last post: by
12 posts views Thread by Sean | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.