This article describes a workaround for Python's garbled behavior when traversing the output name of a zip file. Share to everyone for your reference. Specific as follows:
Windows uses python2.7 to traverse the zip file after the output file name and other information, console printing Chinese and some punctuation is garbled. I checked the Internet. The encoding for Windows is Cp936,print () function to the system to process printing, so it is encoded in advance to the encoding that Windows can recognize.
This print garbled also appears in the form of print (MyList) (MyList is a python list type variable, print (Mylist[2]) is not garbled, strange)
The code is as follows: (add #-*-coding:utf-8–*-to the file header in the. py file)
Import zipfiledef Listzipfilesinfo (path): z=zipfile. ZipFile (path, ' R ') try: for filename in z.namelist (): bytes=z.read (filename) print (' file:%s size:% S '% (Unicode (filename, ' cp936 '). Decode (' Utf-8 '), Len (bytes))) finally: z.close ()
Note: Decode can be removed
Hopefully this article will help you with Python programming.