Decompress the compressed file Python script in the folder

Source: Internet
Author: User

Download a lot of compressed files and write a script

The WinRAR command is used in Python, so generally compressed files are supported.

Some compressed files have no corresponding libraries in Python.


You must add WinRAR to the path environment variable first.


Save the code as RAR. py

Used in DOS, such as RAR. py "D:/a B/C" mkdir


# RAR. PY <br/> # decompress with WinRAR <br/> # arguments: filename directory opt <br/> # opt = 'mkdir' to create directory with the correspond filename <br/> # opt = 'direct' to decompress RAR files in current directory <br/> # opt = 'mk & del 'to mkdir and delete RAR file <br/> Import OS <br/> Import sys <br/> If Len (sys. argv )! = 3: <br/> Print ('wrong arguments/N') <br/> Print ('rar. PY directory opt/N') <br/> Print ('opt =/'mkdir/'to create directory with the correspond filename/N ') <br/> Print ('opt =/'direct/'to decompress RAR files in current directory/N ') <br/> Print ('opt =/'mk & del/'to mkdir and delete RAR file/N') <br/> exit (0) </P> <p> #-ibck, minimized when running <br/> opt = sys. argv [2] <br/> OS. chdir (sys. argv [1]) <br/> for file in OS. listdir ('. '): <br/> If OS. path. isfile (File) and OS .path.splitext(file=%1}{'.rar ': <br/> If opt = 'mkdir ': <br/> cmd = 'winrar X-ibck "'+ file +'" '+' "'+ OS. path. splitext (File) [0] + '"//' <br/> OS. system (CMD) <br/> Elif opt = 'direct ': <br/> cmd = 'winrar X-ibck "'+ file +'" '<br/> OS. system (CMD) <br/> Elif opt = 'mkdel ': <br/> cmd = 'winrar X-ibck "'+ file +'" '+' "'+ OS. path. splitext (File) [0] + '"//' <br/> OS. system (CMD) <br/> OS. remove (File) <br/> else: <br/> Print ('wrong option') </P> <p>

Related Article

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: 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.