How can I write lines to stdout on a Windows machine without having '\n'
expanded to '\r\n'.
I need to do this on Python 2.1 and 2.3+.
I see the msvcrt.setmode function. Is this my only path? Is it valid to
change the mode of stdout? The file.newlines is not writable. 7 7568
Paul Watson wrote: How can I write lines to stdout on a Windows machine without having '\n' expanded to '\r\n'.
I need to do this on Python 2.1 and 2.3+.
I see the msvcrt.setmode function. Is this my only path? Is it valid to change the mode of stdout? The file.newlines is not writable.
What about opening the file in binary mode? This should give you control
over the line endings.
Reinhold
--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
On Wed, 23 Jun 2004, Reinhold Birkenfeld wrote: Paul Watson wrote: How can I write lines to stdout on a Windows machine without having '\n' expanded to '\r\n'.
I need to do this on Python 2.1 and 2.3+.
I see the msvcrt.setmode function. Is this my only path? Is it valid to change the mode of stdout? The file.newlines is not writable.
What about opening the file in binary mode? This should give you control over the line endings.
Believe it or not, open('CON:','wb') actually works under WinXP. It's
amazing that this relic from DOS is still around. Though unportable (the
Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.
Optimally, you'd want to use something like C's freopen to re-open
sys.stdout in binary mode, but I can't find anything like it under the os
module. Does Python not have this ability?
> Believe it or not, open('CON:','wb') actually works under WinXP. It's amazing that this relic from DOS is still around. Though unportable (the Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.
I correct myself. 'CON:' isn't really stdout, but rather the console. So
this won't work if you connect stdout to something.
"Christopher T King" <sq******@WPI.EDU> wrote in message
news:Pi**************************************@ccc8 .wpi.edu... On Wed, 23 Jun 2004, Reinhold Birkenfeld wrote:
Paul Watson wrote: How can I write lines to stdout on a Windows machine without having
'\n' expanded to '\r\n'.
I need to do this on Python 2.1 and 2.3+.
I see the msvcrt.setmode function. Is this my only path? Is it valid
to change the mode of stdout? The file.newlines is not writable. What about opening the file in binary mode? This should give you control over the line endings.
Believe it or not, open('CON:','wb') actually works under WinXP. It's amazing that this relic from DOS is still around. Though unportable (the Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job
done. Optimally, you'd want to use something like C's freopen to re-open sys.stdout in binary mode, but I can't find anything like it under the os module. Does Python not have this ability?
This will be a command to run a python script from inside another tool.
It -must- write to stdout. Also, Cygwin is on the machine.
Even when I try to use low-level I/O it does CRLF interpretation. What am I
doing wrong?
pwatson [ watsonp:/cygdrive/c/src/projects/pwatson/bin ] 22
$ cat ./wb.py
#! /usr/bin/env python
import sys
import os
try:
import msvcrt
f = os.open(1, 'wb')
except:
f = sys.stdout
f.write("now\n")
f.close()
sys.exit(0)
pwatson [ watsonp:/cygdrive/c/src/projects/pwatson/bin ] 23
$ ./wb.py >jjj
pwatson [ watsonp:/cygdrive/c/src/projects/pwatson/bin ] 24
$ od -c jjj
000000 6e 6f 77 0d 0a
n o w \r \n
000005
Paul Watson wrote: Even when I try to use low-level I/O it does CRLF interpretation. What am I doing wrong?
Are you using Cygwin? It will automatically CRLFify anything it thinks
should be (I'm not sure what the exact critera are); this has bit me
before, too. There's an option to disable it in Cygwin setup. This is
being passed on to the script because the stdout redirections are done
in Cygwin, not in Python.
"Chris King" <sq******@wpi.edu> wrote in message
news:10*************@corp.supernews.com... Paul Watson wrote: Even when I try to use low-level I/O it does CRLF interpretation. What
am I doing wrong?
Are you using Cygwin? It will automatically CRLFify anything it thinks should be (I'm not sure what the exact critera are); this has bit me before, too. There's an option to disable it in Cygwin setup. This is being passed on to the script because the stdout redirections are done in Cygwin, not in Python.
The following appears to work under 2.1 in Cygwin and 2.3.4 under a DOS box.
I did not change any Cygwin setting. Is there anything undesireable in the
code below? The intent is to say if this is running on Windows, set stdout
to binary mode.
Am I safe in assuming that 'import msvcrt' will fail on all other machines?
Is there a better way?
$ cat ./wb.py
#! /usr/bin/env python
import sys
import os
try:
import msvcrt
msvcrt.setmode(1, os.O_BINARY)
except:
pass
sys.stdout.write("now\n")
print "and then"
$ ./wb.py >jjj
$ od -c jjj
000000 6e 6f 77 0a 61 6e 64 20 74 68 65 6e 0a
n o w \n a n d t h e n \n
00000d
On Wed, 23 Jun 2004 15:00:02 -0400, rumours say that Christopher T King
<sq******@WPI.EDU> might have written: Believe it or not, open('CON:','wb') actually works under WinXP. It's amazing that this relic from DOS is still around. Though unportable (the Unix equivalent is open('/dev/stdout','wb')) and ugly, it'll get the job done.
/dev/stdout I believe is a Linux device. /dev/tty is the traditional
Unix device for reading/writing to the current "tty".
--
TZOTZIOY, I speak England very best,
"Tssss!" --Brad Pitt as Achilles in unprecedented Ancient Greek This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Mr. Costington |
last post by:
Hi all, I was trying to hava a CGI script return an image, but it was coming
back corrupted. So I tried this simple non-CGI test and still get errors:
import sys
image =...
|
by: Evgeni Sergeev |
last post by:
While I can make a verbatim copy of a file like this:
file1 = file('ready.pdf', 'rb')
file2 = file('out.pdf', 'wb')
buffer = file1.read(256)
while buffer:
file2.write(buffer)
buffer =...
|
by: Mike |
last post by:
I would like my 'print' statements to send its output to the user's
screen and a log file.
This is my initial attempt:
class StdoutLog(file):
def __init__(self, stdout,...
|
by: Rouben Rostamian |
last post by:
Consider the following demo program:
#include <stdio.h>
int main(void)
{
unsigned char c;
for (c=0; c<15; c++)
putchar(c);
|
by: John Forkosh |
last post by:
I have a C program that writes binary output to stdout,
which works fine in Unix/Linux. But when someone else
compiled and ran it in Windows, every time the program
emitted a 0x0A, Windows...
|
by: Charles F McDevitt |
last post by:
I'm converting some old programs that use old iostreams.
In one program, the program is using cout to output to the stdout stream.
Part way through, the program wants to put some binary data out,...
|
by: Anthony Liu |
last post by:
There are many ways to represent a binary tree on an
ascii screen.
1
/ \
2 3
/ \ / \
4 5 6 7
or
|
by: hemant.gaur |
last post by:
I have an application which writes huge number of bytes into the
binary files which is just some marshalled data.
int len = Data.size(); //arrary size
for (int i = 0; i < len; ++i)...
|
by: Bartc |
last post by:
The stdin/stdout files of C seem to be always in Text mode.
Is there any way of running a C program so that these (but especially
stdout) are in Binary mode instead?
(I'm in the process of...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |