LoginSignup
1
0

Pythonで、lzhファイルを解凍する方法(Win版・7z利用)

Posted at

背景

python上でlzhファイルを解凍する必要性が出てきたが、標準ライブラリで解凍する機能が備わっておらず、ググるとよく出てくるライブラリ(lhafile)もインポートできなくて困っていた。
いろいろ探って試した結果、以下のやり方が簡単かなと思った。

準備

7zのサイト上のhowtoページにあるexeファイルをダウンロードし、任意の場所に置いておく。コードの例では、実行ファイルと同じ階層においている。
https://7-zip.opensource.jp/howto/9.20/7z.exe

コード

import subprocess

# 解凍したい対象のファイルパスを変数に入れる
targetfile = r"C:\Temp\test.lzh"

# subprocessで7z.exeを呼び出し、引数にe(解凍の命令)とファイルパスを指定する
subprocess.run("7z.exe e " + targetfile)
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0