472,331 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

clarification on open file modes

Does a py script written to open and read binary files on Windows affect
files on a Linux or Mac machine in a negative way? My concern is
portability and safety. I want scripts written within Windows to work
equally well on Linux and Mac computers.

Is this the safest, most portable way to open files on any platform:

fp = open(file_name, 'rb')
fp.close()

I understand that doing the following on Windows to a binary file (a
jpeg or exe files for example) can cause file corruption, is that correct?

fp = open(file_name, 'r')
fp.close()

How can a simple open in read mode corrupt data???
Jan 5 '07 #1
2 1789
At Thursday 4/1/2007 23:46, tubby wrote:
>I understand that doing the following on Windows to a binary file (a
jpeg or exe files for example) can cause file corruption, is that correct?

fp = open(file_name, 'r')
fp.close()

How can a simple open in read mode corrupt data???
You can't corrupt *that* file if you only open it. But if you *read*
it and process the input data, you'll get some garbage. And if you
write such data (to the same file or another one), it will be corrupted.
Using "rb" or "wb" for binary files won't do any harm on systems
where it doesn't matter, and it's the right way on systems where it does.
--
Gabriel Genellina
Softlab SRL


__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 5 '07 #2
On 2007-01-05 03:46, tubby wrote:
Is this the safest, most portable way to open files on any platform:

fp = open(file_name, 'rb')
fp.close()

I understand that doing the following on Windows to a binary file (a
jpeg or exe files for example) can cause file corruption, is that correct?

fp = open(file_name, 'r')
fp.close()
Rule of thumb: If the file is a text file (usually has a concept
of "text lines"), read and write it in text mode (without "b" in
the mode argument). So line endings will be converted, so that
your Python code can process the text data identically on all
platforms. On the other hand, if you process binary files (as you
mention, for example jpg or exe files), read and write them in
binary mode, so the data won't be corrupted.
How can a simple open in read mode corrupt data???
You won't corrupt the data in the file, but you will kind of
corrupt the data that arrives in your program.

Stefan
Jan 7 '07 #3

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

Similar topics

3
by: Murasama | last post by:
Hi there, Im trying a simple file IO operation in Visual Studio .NET 2003 and it can't seem to open the file. If I run the exe in the debug...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have...
2
by: nissiml | last post by:
hi, i'm trying to open a asp.net web page that list files from a Windows application like winword and select a file from it . what do i have to...
2
by: Mattbooty | last post by:
Hello, Not sure if anyone else has seen this bug, but I have a form where the entire form is covered with a picturebox. The picturebox has a...
2
by: agphoto | last post by:
There is big or problem in open file in read and write mode.. $file = "data.txt"; $fp = fopen($file,"w+"); $line = fgets($fp,"120"); // i need...
5
by: erikcw | last post by:
Hi all, I've created a script that reads in a file, replaces some data (regex), then writes the new data back to the file. At first I was...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a...
3
by: Ecohouse | last post by:
I know how to use the File Dialog control but I'm not sure how to actually open the file that is selected. Here's my code: 'Create a FileDialog...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.