Python solves the Chinese garbled method when fedora extracts zip _python

Source: Internet
Author: User
Tags zip

Objective

Many times in Windows compressed file is no problem, but to Linux, there are garbled, very common. It used to `unzip -O GBK filename.zip` be done under Ubuntu. After the change of Fedora, temporarily did not find garbled compressed files. Download a book in the evening CD, and ran into garbled. The method before the attempt did not succeed. See the next unzip help, no-o that parameter = = just find a way to use Python solution, share.

Create a new file with a '. py ' suffix and copy and paste the code directly:

#!/usr/bin/env python
#-*-coding:utf-8-*-
 
import os
import sys
import zipfile
 
print ' processing File "+ sys.argv[1]
 
file=zipfile. ZipFile (Sys.argv[1], "R");
For name in File.namelist ():
  utf8name=name.decode (' GBK ')
  print "extracting" + utf8name
  pathname = Os.path.dirname (utf8name)
  if not os.path.exists (pathname) and pathname!= "":
    os.makedirs (pathname)
  data = File.read (name)
  if not os.path.exists (utf8name):
    fo = open (Utf8name, "w")
    fo.write (data)
    Fo.close
File.close ()

Execute the zip file and the lovely Chinese comes out.

Python filename. py needs to extract the filename. zip

Summarize

Well, this problem is so simple to solve, we have learned it? Hope this article for everyone's study or work can bring some help, if there is doubt you can message exchange.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.