[Python] When I tried to make a decompression tool with a zip file I just knew, I was addicted to sys.exit ()

Introduction

Knowing that Python has a zipfile module, (While knowing the free software for zip decompression and the standard zip decompression function for Windows) I decided to make a simple decompression tool with the following specifications.

--Drop the zip file into a DOS batch and unzip it --Python on Windows cannot be dropped directly into a script (* .py) --The output folder path is the full path of the zip file with the extension removed. --Input: [Arbitrary folder] \ [Arbitrary name] .zip --Output: [Arbitrary folder] \ [Arbitrary name] --The DOS window will close immediately after the decompression process is completed normally. --If the decompression process ends abnormally, the DOS window will display the exit code and wait for input.

Then I called it with the intention of C # ʻApplication.exit () I've been addicted tosys.exit ()` for a few minutes, so make a note of it.

Completed form

For the time being, I will show you the completed form first. It is assumed that Python is installed and Path is passed. The file name of each script is fixed, but anything is fine as long as it is consistent with the description in the DOS batch.

extract.bat


@echo off
cd /d "%~dp0"

::"Extract" in the same hierarchy as this batch.Premise that there is "py"
python extract.py -i "%~1"

if errorlevel 1 (
	echo.Decompression failed.
	echo.ExitCode:%errorlevel%
	pause
)

extract.py


from argparse import ArgumentParser
import os
import sys
import zipfile

def main():
	parser = ArgumentParser()
	parser.add_argument("-i", "--input", default=False, help="Input Zip")
	args = parser.parse_args()
	filePath = args.input
	
	if (not filePath):
		sys.exit(1)
	if (not os.path.isfile(filePath)):
		sys.exit(2)
	
	folderName = os.path.splitext(os.path.basename(filePath))[0]
	outputPath = os.path.join(os.path.dirname(filePath), folderName)
	
	try:
		with zipfile.ZipFile(filePath) as zip:
			zip.extractall(outputPath)
	except:
		sys.exit(3)
	else:
		sys.exit(0)

if (__name__ == "__main__"):
	main()

I was addicted to

To tell you the truth, I continued to be addicted to it without knowing the following. -[Python] sys.exit () just throws an exception

It's faster to look at the code to see how you got hooked.

What I was addicted to: Part 1

Even at the time of normal termination, the exit code is now 3. .. ..

		try:
			with zipfile.ZipFile(filePath) as zip:
				zip.extractall(outputPath)
			sys.exit(0)
		except:
			sys.exit(3)

What I was addicted to: Part 2

Even at the time of abnormal termination, the exit code became 0. .. ..

		try:
			with zipfile.ZipFile(filePath) as zip:
				zip.extractall(outputPath)
		except:
			sys.exit(3)
		finally:
			sys.exit(0)

At the end

Thanks to my addiction, I understand how Python try-except-else-finally works.

Recommended Posts

[Python] When I tried to make a decompression tool with a zip file I just knew, I was addicted to sys.exit ()
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
Python: I tried to make a flat / flat_map just right with a generator
A note I was addicted to when running Python with Visual Studio Code
A story that I was addicted to when I made SFTP communication with python
I tried to make a periodical process with Selenium and Python
A note I was addicted to when creating a table with SQLAlchemy
I tried to make a 2channel post notification application with Python
I tried to make a todo application using bottle with python
When I tried to scrape using requests in python, I was addicted to SSLError, so a workaround memo
A story I was addicted to when inserting from Python to a PostgreSQL table
I was addicted to creating a Python venv environment with VS Code
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I was addicted to trying Cython with PyCharm, so make a note
I want to make a game with Python
I want to write to a file with Python
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
When I tried to make a VPC with AWS CDK but couldn't make it
[Patent analysis] I tried to make a patent map with Python without spending money
When I tried to create a virtual environment with Python, it didn't work
Three things I was addicted to when using Python and MySQL with Docker
Try to make a command standby tool with python
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I was addicted to scraping with Selenium (+ Python) in 2020
I tried to automatically generate a password with Python3
What I was addicted to when using Python tornado
I tried to make a real-time sound source separation mock with Python machine learning
I tried to make various "dummy data" with Python faker
What I was addicted to when migrating Processing users to Python
I tried to make a stopwatch using tkinter in python
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried to divide the file into folders with Python
[1 hour challenge] I tried to make a fortune-telling site that is too suitable with Python
When I tried to install PIL and matplotlib in a virtualenv environment, I was addicted to it.
I tried uploading / downloading a file to AWS S3 / Azure BlobStorage / GCP CloudStorage with Python
What I was addicted to when dealing with huge files in a Linux 32bit environment
I tried to make a generator that generates a C # container class from CSV with Python
I tried to convert a Python file to EXE (Recursion error supported)
Rubyist tried to make a simple API with Python + bottle + MySQL
A memorandum when I tried to get it automatically with selenium
I tried to make a regular expression of "amount" using Python
What I was addicted to when introducing ALE to Vim for Python
[Python] I tried to implement stable sorting, so make a note
I tried to make a regular expression of "time" using Python
What I was addicted to with json.dumps in Python base64 encoding
[Python] A memo that I tried to get started with asyncio
A note I was addicted to when making a beep on Linux
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
[Python] Simple Japanese ⇒ I tried to make an English translation tool
I tried to make a strange quote for Jojo with LSTM
I tried to make an image similarity function with Python + OpenCV
I tried to make a mechanism of exclusive control with Go
When I tried to use pip with python, I was told that XML_SetHashSalt could not be found.