You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Support for 7zip files in Python is limited. Although pandas and other data processing libraries have the ability to directly read gzip and bz2 files, some of the compression algorithms used by 7zip are not well supported by available Python modules. Regardless, the ability to call 7zip from the command line does have some use cases.</p>
<p>(Note that the <ahref="https://docs.python.org/3/library/lzma.html">lzma module</a> in the standard library may already be a solution to many problems and should be tried first)</p>
<p>First, let's try creating a sample 7z file using the command line. The <em>subprocess</em> module in the standard library is one way to call the 7zip program directly, as if it were on the command line. This code will write to a temporary directory in C:\Temp and assumes that you are using Windows. Please modify the temp_dir accordingly if you are using another operating system. Also, please modify the exact location of 7zip on your system using the loc_7z variable. Alternatively, if the 7zip location has been added to the system path, you may just use "7z" or "7z.exe".</p>
<spanclass="c1"># Now use the subprocess command to run 7zip. Warning- all paths should be </span>
<spanclass="c1"># enclosed in quotation marks just in case there are any spaces in the path</span>
<spanclass="c1"># The "a" command given to 7z.exe means "add to archive"</span>
<spanclass="n">archive_command</span><spanclass="o">=</span><spanclass="sa">r</span><spanclass="s1">'"</span><spanclass="si">{}</span><spanclass="s1">" a "</span><spanclass="si">{}</span><spanclass="s1">" "</span><spanclass="si">{}</span><spanclass="s1">"'</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">loc_7z</span><spanclass="p">,</span><spanclass="n">dummy_out_path</span><spanclass="p">,</span><spanclass="n">dummy_file_path</span><spanclass="p">)</span>
<p>The actual line of command-line code that was called using subprocess above is:</p>
<p><em>"C:\Program Files\7-Zip\7z.exe" a "C:\Temp\dummy.7z" "C:\Temp\dummy.txt"</em></p>
<p>unless any of the default variables were changed. The "a" command given to 7z.exe is used to make an archive, and the first argument afterwards is the output location of the archive, and the second argument is the location of the original file. Check the path "C:\Temp\dummy.7z" and you will see the file you just created. Note that this path will be different if you changed the temp_dir or dummy_out_path variables above.</p>
<p>There are two common problems that may be encountered here. First, the 7zip program may not always be added to the system PATH. It's up to you if you would like to do so, but if you don't know what modifying the PATH variable means, you should probably just specify the full path to the 7z program. This is usually something like "C:\Program Files\7-zip\7z.exe" on Windows, but you can check your default path by trying to re-install the 7zip program. Second, it appears that you may be able to only run one command-line version of 7zip at a time. If you are trying to run multiple command-line programs at once, or if you already have an unzip operation running, this code may not work on your system.</p>
<p>Now, let's extract the archive that was created above.</p>
<divclass=" highlight hl-ipython3"><pre><span></span><spanclass="c1"># The "e" command given to 7z.exe means "extract from archive"</span>
<spanclass="c1"># WARNING- One of the most common mistakes is that there is NO space between the</span>
<spanclass="c1"># -o flag and the actual output directory (e.g. it should look like: "-oC:\Temp")</span>
<spanclass="n">extract_command</span><spanclass="o">=</span><spanclass="sa">r</span><spanclass="s1">'"</span><spanclass="si">{}</span><spanclass="s1">" x "</span><spanclass="si">{}</span><spanclass="s1">" -o"</span><spanclass="si">{}</span><spanclass="s1">"'</span><spanclass="o">.</span><spanclass="n">format</span><spanclass="p">(</span><spanclass="n">loc_7z</span><spanclass="p">,</span><spanclass="n">dummy_out_path</span><spanclass="p">,</span><spanclass="n">temp_dir</span><spanclass="p">)</span>
<p>The actual line of command-line code that was called using subprocess above is:</p>
<p><em>"C:\Program Files\7-Zip\7z.exe" x "C:\Temp\dummy.txt" -o"C:\Temp"</em></p>
<p>Important note- there is no space in between the -o and "C:\Temp"; this is not a typo. Take a look at the "C:\Temp" directory and you will notice that the dummy.txt file has been successfully extracted from the dummy.7z file!</p>