473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

piping out binaries properly

I have got following program:

import sys
import binascii
from string import *
sys.stdout.write(binascii.unhexlify("41410A4141"))
when I run under Unix I got:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0a 41 41

and under Windows/Cygwin following:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0d 0a 41 41
0000006
The question is how can I pipe out binary content properly and platform
independently?
A.
Oct 12 '05 #1
9 1989
Andy Leszczynski <leszczynscyATnospam.yahoo.com.nospam> writes:
I have got following program:

import sys
import binascii
from string import *
sys.stdout.write(binascii.unhexlify("41410A4141"))
when I run under Unix I got:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0a 41 41

and under Windows/Cygwin following:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0d 0a 41 41
0000006

The question is how can I pipe out binary content properly and platform
independently?


It's not normal to write binary content to stdout - you normally write
it to a file. Open the file with open(name, 'wb') to write binaries.

There doesn't appear to be any way to retroactively change the mode on
a file. Which is probably a good thing.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 12 '05 #2
Mike Meyer wrote:
It's not normal to write binary content to stdout - you normally write


Well, I grew up in the Unix world and it is normal over there.

I am still curious which layer adds that 0xd. Is it python, cygwin,
windows ...

Thx for reply, Andy
Oct 12 '05 #3
On Wed, 2005-10-12 at 00:16 -0400, Mike Meyer wrote:
[...]
It's not normal to write binary content to stdout - you normally write
it to a file. Open the file with open(name, 'wb') to write binaries.

It is interesting that as a "Unix consultant" you should make that
claim. Especially since
sys.stdout

<open file '<stdout>', mode 'w' at 0x2aaaaaac9198>
^^^^

Indeed there would be a lot of broken Unix systems out there if that
were not the case.

As for the OP, you may want to check how stdout is opened on your
system. In Windows there are two "write" modes for files, 'w', and 'wb'
where apparently 'w' is text mode and 'wb' is binary. You may want to
check what mode your stdout is in. I don't have a Windows box handy
right now to verify.
There doesn't appear to be any way to retroactively change the mode on
a file. Which is probably a good thing.

<mike--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more

information.
Oct 12 '05 #4
Andy Leszczynski <leszczynscyATnospam.yahoo.com.nospam> writes:
Mike Meyer wrote:
It's not normal to write binary content to stdout - you normally write
Well, I grew up in the Unix world and it is normal over there.


I watched the Unix world grow up, and it ain't normal to me. I don't
think I've ever written a program that wrote binary data to standard
out, not in nearly 30 years of unix programming. I've written lots of
things whose standard out was designed specifically to be read by
another program, but never as binary data.
I am still curious which layer adds that 0xd. Is it python, cygwin,
windows ...


It's Python. You tell Python whether or not it needs to do
platform-dependent newline mangling when you open the file.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 12 '05 #5
Mike Meyer wrote:
I watched the Unix world grow up, and it ain't normal to me.
Since there's no distinction between a file opened in binary mode and in
text mode on Unix, there is no difference.
I don't
think I've ever written a program that wrote binary data to standard
out, not in nearly 30 years of unix programming. I've written lots of
things whose standard out was designed specifically to be read by
another program, but never as binary data.


Plenty of applications use that functionality and depend on it. See
cjpeg, djpeg, the pbmplus library, and so forth.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Every human being is a problem in search of a solution.
-- Ashley Montagu
Oct 12 '05 #6
marduk <us****@marduk.letterboxes.org> writes:
On Wed, 2005-10-12 at 00:16 -0400, Mike Meyer wrote:
[...]
It's not normal to write binary content to stdout - you normally write
it to a file. Open the file with open(name, 'wb') to write binaries.


It is interesting that as a "Unix consultant" you should make that
claim. Especially since
sys.stdout

<open file '<stdout>', mode 'w' at 0x2aaaaaac9198>
^^^^

Indeed there would be a lot of broken Unix systems out there if that
were not the case.


Ain't ambiguity wonderful? That we use the same word to donote a
collection of bytes on a disk and a software entity that one can write
bytes to allows for all kind of mistaken interpretations.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Oct 12 '05 #7
Erik Max Francis <ma*@alcyone.com> writes:
I've written lots of things whose standard out was designed
specifically to be read by another program, but never as binary data.


Plenty of applications use that functionality and depend on it. See
cjpeg, djpeg, the pbmplus library, and so forth.


Also "tar cf files... | compress > tarball.Z" used to be a standard
idiom, now replaced by "tar cfz ..." which does about the same thing under
the covers.
Oct 12 '05 #8
Andy Leszczynski wrote:
when I run under Unix I got:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0a 41 41

and under Windows/Cygwin following:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0d 0a 41 41
0000006

The question is how can I pipe out binary content properly and platform
independently?


$ python -h
usage: python [option] ... [-c cmd | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
....
-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERED=x)
....

</F>

Oct 12 '05 #9
"Andy Leszczynski" <leszczynscyATnospam.yahoo.com.nospam> wrote in message
news:o_********************@comcast.com...
I have got following program:

import sys
import binascii
from string import *
sys.stdout.write(binascii.unhexlify("41410A4141"))
when I run under Unix I got:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0a 41 41

and under Windows/Cygwin following:

$ python u.py > u.bin
$ od -t x1 u.bin
0000000 41 41 0d 0a 41 41
0000006
The question is how can I pipe out binary content properly and platform
independently?
A.


try:
import msvcrt, os
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
except:
pass
Oct 12 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tefla | last post by:
Hi folks, i'm writing a simple tool for people in my work place, so they can easily access an SubVersioN repository. basicly, all the developers and designers have apache and a local version...
4
by: Christian Long | last post by:
Hi I'm trying to pipe data into a python program on Windows 2000, on the command line. Like this: dir | myProgram.py Here's what I tried:
2
by: Apple Grew | last post by:
I want to develope a Winboard like application which will support Winboard protocol. For that I require to know how to handle piping in Python. I seperated out module popen2, but when I use...
2
by: Christopher Subich | last post by:
From the documentation, it looks like DParser-python will do what I need, but I'm having trouble getting it installed properly. I'm using a win32 environment, with official 2.4 Python binaries. ...
5
by: Jan Dries | last post by:
I'm trying to find Windows binaries for lxml. The cheeseshop is supposed to have such binaries, but I can't find them. Does anyone know where I might find such binaries? Thanks, Jan
2
by: moti | last post by:
I am a newbie to MySQL. I'm confused as to the piping syntax of SERVER in the User DSN in ODBC for a client PC. If I use an ip it does not work, probably because of a firewall. So I tried using...
3
by: noob2008 | last post by:
can anyone explain me what is piping in C++ programming? for example invoice.exe < purchase.dat > statement.txt do i replace that where all the cin statements? i created a text file called...
4
by: samit | last post by:
I keen to persue coding like yahoo piping system which will include drag and drop and piping features
0
by: jsimps44 | last post by:
Hi, I'm fairly new to c, and very new to piping and file descriptors and can't seem to get past this problem. The piping is very much not working, and I can't figure out for the life of me why. Any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.