Re: Reading binary files

2009-08-30 Thread Dan Stromberg

David Robinow wrote:

This works for a simple binary file, but the actual file I'm trying to
read is give throwing an error that the file cannot be found. Here is the
name of the my file:
2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars

Should python have trouble reading this file name or extension?
  

I'm having trouble with the filename:
2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars

It throws an error with that file name, When I change it to something like
sample.txt it runs, but the data is still garbled. Is there any reason why I
can't use the above file name? If I'm using 'rb' to read the binary file why
is it still garbled?


 I don't think it's garbled. It's a binary file. What do you expect?

It's been over ten years since I've worked with any JSTARS stuff so I
can't give you any details but you almost certainly have some sort of
imagery. The military has a lot of bizarre formats and whoever sent
you the data should have included a data sheet describing the format
(or a pointer to such).  Ideally, you'll also get a pointer to code to
read the thing, but sometimes you just have to bite the bullet and
write a program to process the file.
   (If all else fails, look at a dump of the first 512 bytes or so;
often the image size is included at the beginning; maybe in ASCII, 16
bit ints, 32 bit ints, floating point -- who knows)
There've been times when I had to just display the thing at 512 or
1024 bytes (or ints) per row and try to surmise the info from that.
So, look for the file description.
...
Googling a bit:   I see there's a package at
   http://www.mbari.org/data/mbsystem/index.html
which purports to handle some JSTARS stuff. I've no idea if that will help you.
  
If you don't find anything preexisting for reading JSTARS format, this 
might help:


http://stromberg.dnsalias.org/~strombrg/converting-binary.html

--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2009-08-24 Thread David Robinow
>> This works for a simple binary file, but the actual file I'm trying to
>> read is give throwing an error that the file cannot be found. Here is the
>> name of the my file:
>> 2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars
>>
>> Should python have trouble reading this file name or extension?
>
> I'm having trouble with the filename:
> 2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars
>
> It throws an error with that file name, When I change it to something like
> sample.txt it runs, but the data is still garbled. Is there any reason why I
> can't use the above file name? If I'm using 'rb' to read the binary file why
> is it still garbled?
 I don't think it's garbled. It's a binary file. What do you expect?

It's been over ten years since I've worked with any JSTARS stuff so I
can't give you any details but you almost certainly have some sort of
imagery. The military has a lot of bizarre formats and whoever sent
you the data should have included a data sheet describing the format
(or a pointer to such).  Ideally, you'll also get a pointer to code to
read the thing, but sometimes you just have to bite the bullet and
write a program to process the file.
   (If all else fails, look at a dump of the first 512 bytes or so;
often the image size is included at the beginning; maybe in ASCII, 16
bit ints, 32 bit ints, floating point -- who knows)
There've been times when I had to just display the thing at 512 or
1024 bytes (or ints) per row and try to surmise the info from that.
So, look for the file description.
...
Googling a bit:   I see there's a package at
   http://www.mbari.org/data/mbsystem/index.html
which purports to handle some JSTARS stuff. I've no idea if that will help you.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2009-08-24 Thread Dave Angel

Ronn Ross wrote:

On Mon, Aug 24, 2009 at 11:18 AM, Ronn Ross  wrote:

  

On Mon, Aug 24, 2009 at 10:43 AM, Albert Hopkins wrote:



On Mon, 2009-08-24 at 10:35 -0400, Ronn Ross wrote:
  

I need to read a binary file. When I open it up in a text editor it is
just junk. Does Python have a class to help with this?


Yes, the "file" class.

  

myfile = open('/path/to/binary/file', 'rb')


-a

  

This works for a simple binary file, but the actual file I'm trying to read
is give throwing an error that the file cannot be found. Here is the name of
the my file:
2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars

Should python have trouble reading this file name or extension?



I'm having trouble with the filename:
2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars

It throws an error with that file name, When I change it to something like
sample.txt it runs, but the data is still garbled. Is there any reason why I
can't use the above file name? If I'm using 'rb' to read the binary file why
is it still garbled?


  
You're describing two separate problems here.  One is an error in the 
open statement, and the other is "something is garbled."



If you hand a relative path name to the open() function, it'll have to 
get the absolute path from somewhere else, presumably the current 
directory.  Do you know for sure what the current directory is, and is 
that file located there?  Try printing  os.path.abspath(os.curdir).   Or 
pass a complete (absolute) path to open().


As for garbled...   Just what do you expect?  A binary file is a 
completely arbitrary sequence of bytes.  So if you're trying to print 
it, you may see "garbled."  Just what is the file's purpose anyway?  You 
can't do much with it without knowing something about it.


DaveA




--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2009-08-24 Thread Ronn Ross
On Mon, Aug 24, 2009 at 11:18 AM, Ronn Ross  wrote:

>
>
> On Mon, Aug 24, 2009 at 10:43 AM, Albert Hopkins 
> wrote:
>
>> On Mon, 2009-08-24 at 10:35 -0400, Ronn Ross wrote:
>> > I need to read a binary file. When I open it up in a text editor it is
>> > just junk. Does Python have a class to help with this?
>>
>> Yes, the "file" class.
>>
>> >>> myfile = open('/path/to/binary/file', 'rb')
>>
>> -a
>>
> This works for a simple binary file, but the actual file I'm trying to read
> is give throwing an error that the file cannot be found. Here is the name of
> the my file:
> 2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars
>
> Should python have trouble reading this file name or extension?
>
I'm having trouble with the filename:
2009.08.02_06.52.00_WA-1_0001_00_0662_0.jstars

It throws an error with that file name, When I change it to something like
sample.txt it runs, but the data is still garbled. Is there any reason why I
can't use the above file name? If I'm using 'rb' to read the binary file why
is it still garbled?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2009-08-24 Thread Albert Hopkins
On Mon, 2009-08-24 at 10:35 -0400, Ronn Ross wrote:
> I need to read a binary file. When I open it up in a text editor it is
> just junk. Does Python have a class to help with this? 

Yes, the "file" class.

>>> myfile = open('/path/to/binary/file', 'rb')

-a


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2009-08-24 Thread Rami Chowdhury
The built-in file type deals with this just fine. You can simply specify  
when opening the file that it is to be opened as binary:


http://docs.python.org/library/functions.html#open

On Mon, 24 Aug 2009 07:35:09 -0700, Ronn Ross  wrote:

I need to read a binary file. When I open it up in a text editor it is  
just

junk. Does Python have a class to help with this?




--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --  
Hanlon's Razor

408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2005-11-23 Thread Fredrik Lundh
"amfr" <[EMAIL PROTECTED]> wrote

> On windows, is there anything special I have to do to read a binary
> file correctly?

the documentation has the answer:

http://docs.python.org/lib/built-in-funcs.html#l2h-25

Append 'b' to the mode to open the file in binary mode, on
systems that differentiate between binary and text files (else it
is ignored). /.../

When opening a binary file, you should append 'b' to the
mode value for improved portability. (It's useful even on
systems which don't treat binary and text files differently,
where it serves as documentation.)

in other words, always use

f = open(filename, "rb")

to open binary files for reading, on all platforms (but it only matters
on some platforms).  to open for writing, use

f = open(filename, "wb") # create new file

or

f = open(filename, "r+b") # open existing file for read/write

 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading binary files

2005-11-23 Thread Grant Edwards
On 2005-11-23, amfr <[EMAIL PROTECTED]> wrote:

> On windows, is there anything special I have to do to read a binary
> file correctly?

Open it in binary mode?

-- 
Grant Edwards   grante Yow!  I didn't order
  at   any WOO-WOO... Maybe a
   visi.comYUBBA... But no WOO-WOO!
-- 
http://mail.python.org/mailman/listinfo/python-list