Windows vs. file.read

M

Mike

I have a ppm file that python 2.5 on Windows XP cannot read
completely.
Python on linux can read the file with no problem
Python on Windows can read similar files.
I've placed test code and data here:
http://www.cs.ndsu.nodak.edu/~hennebry/ppm_test.zip
Within the directory ppm_test, type
python ppm_test.py
The chunk size commentary occurs only if file.read cannot read enough
bytes.
The commentary only occurs for the last file.
Any ideas?
Any ideas that don't require getting rid of Windows?
It's not my option.
 
M

MRAB

I have a ppm file that python 2.5 on Windows XP cannot read
completely.
Python on linux can read the file with no problem
Python on Windows can read similar files.
I've placed test code and data here:
http://www.cs.ndsu.nodak.edu/~hennebry/ppm_test.zip
Within the directory ppm_test, type
python ppm_test.py
The chunk size commentary occurs only if file.read cannot read enough
bytes.
The commentary only occurs for the last file.
Any ideas?
Any ideas that don't require getting rid of Windows?
It's not my option.

You should open the files in binary mode, not text mode, ie file(path,
"rb"). Text mode is the default. Not a problem on *nix because the line
ending is newline.
 
D

David Robinow

I have a ppm file that python 2.5 on Windows XP cannot read
completely.
Python on linux can read the file with no problem
Python on Windows can read similar files.
I've placed test code and data here:
http://www.cs.ndsu.nodak.edu/~hennebry/ppm_test.zip
Within the directory ppm_test, type
python ppm_test.py
The chunk size commentary occurs only if file.read cannot read enough
bytes.
The commentary only occurs for the last file.
Any ideas?
Any ideas that don't require getting rid of Windows?
It's not my option.
Open the files in binary mode. i.e.,
x=Ppm(file("ff48x32.ppm",'rb'))
x=Ppm(file("bw48x32.ppm",'rb'))
x=Ppm(file("bisonfootball.ppm",'rb'))

You were just lucky on the first two files.
 
M

Mike

You should open the files in binary mode, not text mode, ie file(path,
"rb"). Text mode is the default. Not a problem on *nix because the line
ending is newline.

Thanks.
That was it.
 
L

Lawrence D'Oliveiro

MRAB said:
You should open the files in binary mode, not text mode, ie file(path,
"rb"). Text mode is the default. Not a problem on *nix because the line
ending is newline.

We used to pride ourselves on not having to worry about text versus binary
I/O modes on *nix, but I’m beginning to think the reality is we have to
adopt it.

To start with, it means we can automatically handle different newline
conventions with text files originating on different systems.
 
M

MRAB

We used to pride ourselves on not having to worry about text versus binary
I/O modes on *nix, but I’m beginning to think the reality is we have to
adopt it.

To start with, it means we can automatically handle different newline
conventions with text files originating on different systems.

In Python 3 the difference is important because binary mode works with
bytes and text mode works with strings, plus the file encoding and line
endings.
 
L

Lawrence D'Oliveiro

MRAB said:
In Python 3 the difference is important because binary mode works with
bytes and text mode works with strings, plus the file encoding and line
endings.

Yeah, that seems like a reasonable approach—make you decide up-front what
exactly is the sort of file content you’re dealing with.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top