Python solves Chinese garbled characters when decompressing the zip file of Fedora,

Source: Internet
Author: User

Python solves Chinese garbled characters when decompressing the zip file of Fedora,

Preface

It is common to compress files in windows, but garbled characters appear in Linux. Previously, in Ubuntu`unzip -O GBK filename.zip` You can do this. After changing Fedora, no garbled compressed files are found. In the evening, I downloaded a book's CD and encountered garbled characters. The previous method failed. After reading the help of unzip, I didn't have the-O parameter =. I just found a solution using python and shared it.

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

#!/usr/bin/env python# -*- coding: utf-8 -*- import osimport sysimport 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.closefile.close()

Decompress the zip file, and the cute Chinese is displayed.

Python file name. py unzip decompressed file name. Zip

Summary

Well, this is a simple solution. Have you learned it? I hope this article will help you in your study or work. If you have any questions, you can leave a message.

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.