First, my system and Python version information:
Win7, python2.7.12
I've been looking for two articles that I think are better:
The first article http://blog.csdn.NET/luoye7422/article/details/41873499, according to his method to really even the error is the same, but unfortunately, even if the C:\Program files\ The UnRAR.exe in the WinRAR directory is copied under the script folder or added to the environment variable to still report this error, so ... Can only give up, if a small partner in accordance with this method to be able to achieve the best. (In fact, the essence of this method is to encapsulate a popen function, that is, call the cmd Command window to extract, so interested students can directly to find Python call cmd method, and then write a decompression function on their own OK.) )
The second article http://blog.csdn.net/q1w2e3r4470/article/details/51859467, the operation is slightly more complicated than the first one (but it is only relative, in fact, I think the author is just not clear, I have collated it with the information I have consulted.
get to the point.
First step
Install the Unrar module pip install Unrar (don't explain how to use PIP)
Then try to enter the from Unrar import Rarfile and run in the IDE (I'm using pycharm), and it will display an error:
Lookuperror:couldn ' t find path to Unrar library.
It means we can't find the path to the Unrar library, we need to download this Unrar library, in fact it is UnRAR.dll this thing, download URL: Http://www.rarlab.com/rar/UnRARDLL.exe Or go to http://www.rarlab.com/rar_add.htm find UnRAR.dll download, in LUnix should need to compile their own.
Step Two:
The path to my computer after installation is C:\Program Files (x86) \unrardll,win7 32-bit friends can add it to the environment variable, 64-bit to set the X64 folder as the environment variable, Because the Unrar module recognizes files that are Unrar.dll and Unrar.lib, the UnRAR.dll and UnRAR.lib in the folder are renamed in lowercase.
All right, try the from Unrar import Rarfile and run, and it works. (In fact, I still have an error, I put the pycharm again after the boot will be good, may be pycharm update has a delay)
Step Three:
It's finally ready to unpack, and the order is simple.
Rarfile
file = rarfile. Rarfile (' file_name ') #这里写入的是需要解压的文件, don't forget to add the path
file.extractall (' You_want_path ') This is written in the folder you want to extract into.
Go to your designated folder and see if it's there.
If you want to know more about Unrar functions, you can go here: Https://github.com/matiasb/python-unrar
Check out the example below.