Read zip and write it to another python file

I want to read a file and write it back. Here is my code:

   file = open( zipname , 'r' )
   content =  file.read() 
   file.close()

   alt = open('x.zip', 'w')
   alt.write(content )
   alt.close()

It doesn’t work, why ?????

Edit:

The rewritten file is corrupted (python 2.7.1 on windows)

+5
source share
3 answers

Read and write in binary mode, 'rb' and 'wb':

f = open(zipname , 'rb')
content =  f.read() 
f.close()

alt = open('x.zip', 'wb')
alt.write(content )
alt.close()

The reason text mode does not work on Windows is because wrapping the new line from '\ r \ n' to '\ r' has distorted the binary data in the zip file.

+9
source

From this fragment of the manual :

Windows, 'b', , , , "rb", "wb" "r + b". Python Windows ; , . ASCII, itll , JPEG EXE. . Unix "b" , - .

+6

OS X Linux, , . x.zip ​​ , zip , . , Windows , ; :

file = open(zipname, 'rb')
+1
source

All Articles