Decompress multiple compressed files (Python)

Overview

In order to run the game with an emulator called Redream, I had to decompress the compressed file for each directory, so I made a batch.

Pre-work

Install the package to unzip the 7z file. Install 7z with the following command:

sudo apt-get install -y p7zip-full

Python code

Create the following Python code and save it in a batch.py file.

#!/usr/bin/python
# -*- coding: utf-8 -*-
import io
import os
import glob

arr = glob.glob("*7z")
#print(arr)
path_w = '7z.sh'
with open(path_w, mode='w') as f:
    for str in arr:
        dir = str.replace("[GDI] ","").replace("[CDI] ","").replace(".7z","").replace("'","")
        tmpstr = str.replace("'","").replace(",","")
        os.rename("./" + str , "./" + tmpstr)
        try:
            if not os.path.exists(dir):
                cmds = "7z x '" + tmpstr + "' -o'" + dir + "'\n"
                dels = "rm -rf '" + tmpstr + "'\n"
                #print(cmds)
                #print(dels)
                f.write(cmds)
                f.write(dels)
        except OSError:
            print("Error: " + dir)
                                 

Python code description

I didn't write a comment on the source, so I'd like to explain it here.

#!/usr/bin/python
# -*- coding: utf-8 -*-
#This is the part that declares the package to use the provided methods.
import io
import os
import glob

#Only save files with the extension "7z" in the current directory to the array.
arr = glob.glob("*7z")

#The following Print is the code to check the contents of the saved array.
# print(arr)

#Unzip and delete the compressed file into a shell script file
#Specifies the name of the shell script file where the command is saved.
path_w = '7z.sh'

#Open shell script file to write mode
with open(path_w, mode='w') as f:

    #Create a decompression / deletion command for each "7z" file.
    for str in arr:

        #Create the directory name to decompress from the compressed file name.
        #Delete unnecessary character strings in the directory name.
        dir = str.replace("[GDI] ","").replace("[CDI] ","").replace(".7z","").replace("'","")

        #If the file name and directory name have the following character strings, the answer will fail, so
        #Delete the character. And replace the file name as well.
        tmpstr = str.replace("'","").replace(",","")
        os.rename("./" + str , "./" + tmpstr)
        try:

            #If the directory name does not exist, do the following:
            if not os.path.exists(dir):

                #This command decompresses the compressed file.
                cmds = "7z x '" + tmpstr + "' -o'" + dir + "'\n"

                #This command deletes the unzipped files.
                dels = "rm -rf '" + tmpstr + "'\n"

                #This is the code to confirm the command.
                #print(cmds)
                #print(dels)

                #Save the command in a shell script file.
                f.write(cmds)
                f.write(dels)

        except OSError:
            #Output when a directory-related error occurs.
            print("Error: " + dir)

Run python code

Run the Python code with the following command: When executed, a "7z.sh" file will be generated.

pi@raspberrypi:~/Downloads/redream/rom $ python batch.py

Check the contents of the 7z.sh file

Check the contents of the file with the following command. You can see that the decompression / deletion command is generated for each compressed file name.

pi@raspberrypi:~/Downloads/redream/rom $ cat 7z.sh
7z x '[GDI] XXXXXXXX.7z' -o'XXXXXXXX'
rm -rf '[GDI] XXXXXXXX.7z'
7z x '[CDI] XXXXXXXX.7z' -o'XXXXXXXX'
rm -rf '[CDI] XXXXXXXX.7z'

Execute the 7z.sh command

Run the shell script file with the following command: When executed, the screen to decompress the compressed file is displayed. When the execution is finished, you can see that all the compressed files have been deleted. If it is possible that the file name is stopped due to an unexpected character string error, In that case, add replacement to the processing of file name / directory name. Then you should be able to solve it without any problems.

pi@raspberrypi:~/Downloads/redream/rom $ sh 7z.sh

Other

If the capacity of the SD card is insufficient, the decompression of the file may fail, so be careful. (Lol)

At the end

With this, it was easy to put the game in Retropie OS, but Next, the specification of GDI / CDI and preview image file remains in game.xml. When running on Raspbian, it is not necessary to specify the above game.xml. It was troublesome to compress and decompress manually, so I made it. Was it useful? Exception handling is a little lacking, but please add it if necessary.

Recommended Posts

Decompress multiple compressed files (Python)
Combine multiple python files into one python file
[Python & Unix] Combine multiple PDF files into one.
Convert multiple proto files at once with python
[python] Compress and decompress
[Python] Create multiple directories
Python --Tagging MP3 files
[Python] Reading CSV files
Remove headings from multiple format CSV files with python
Multiple regression expressions in Python
[Python] Send gmail with python: Send one by one with multiple image files attached
Rsync multiple files at once
[R] [Python] Memo to read multiple csv files in multiple zip files
Specify multiple list indexes (Python)
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Install multiple versions of Python
Sorting image files with Python
Integrate PDF files with Python
Reading .txt files with Python
Avoid multiple loops in Python
Prohibit multiple launches in python
Upload multiple files in Flask
Extract multiple list duplicates in Python
Transpose CSV files in Python Part 1
[ev3dev × Python] Control of multiple motors
[Python] Show multiple windows in Tkinter
[Python] Loading csv files using pandas
[Blender] Split Blender script into multiple files
Manipulating EAGLE .brd files with Python
Load multiple JavaScript files with PyWebView
Manipulate files and folders in Python
[Python] POST wav files with requests [POST]
Read and use Python files from Python
Handling of JSON files in Python
Download Google Drive files in Python
Decrypt files encrypted with OpenSSL with Python 3
Post multiple Twitter images with python
Sort large text files in Python
Animate multiple still images with Python
Python os related files, subdirectory operations
Handle Excel CSV files with Python
Read files in parallel with Python
Statistical test (multiple test) in Python: scikit_posthocs
Export and output files in Python
Multiple integrals with Python and Sympy
Delete multiple elements in python list
[Python] Creating multiple windows with Tkinter
Sorting files by Python naming convention
Extract strings from files in Python