• 2020-03-10

Python: Output the log with logging module

It is general to export the log from the batch script which is executed periodically. The log is also useful to analyze the problem. You may insert the special log for debug to explore the reason of bug. The log is base facility to explore the reason of problem and recover from it. You can output the log from Python script with logging module. Simple Logging Most simple logging code is following. Execute this script, the following log will be output. How to output the log You can output the log by following functions. You need to select the function for appropriated logging level. info() warning() error() critical() exception() debug() The argument of these functions is a formatted string. The Logging Levels The importance of each logs are different. The logging level is an importance. For example, in production environment, the log is not needed which is output by debug() function. However, in development environment, it is an important for the developer. The output logging level can be specified with the code. Change the output logging level The minimum output logging level is changed with level argument of the basicConfig() method. In this sample code, the logging level of the output is […]

  • 2020-03-08

Python: Get the Command Line Argument with ArgumentParser

It is easy to get the command line argument of python shell script with ArgumentParser class. The ArgumentParser class has following functions: Get the positional required argument Get the optional argument Output the help If the script executed without required arguments, the ArgumentParser class abort the script. It is hard work to write above functions by yourself in your own script. Basic usage The basic code to use `ArgumentParser` class is following: Write your program description You can set the description of your program with the description parameter of the initializer. The description is displayed by --help option. Descriptions of programs and arguments can be omitted. However, we recommend that you do not omit it. Immediately after implementing the code, you know for yourself what the program is. However, after a while you may forget. In addition, if you take over and explain when another person is in charge or when someone uses it, you only need to hand it over. Parse and Get the Required Argument Use add_argument() method to parse and get the required argument. This code add the value argument. You can set the description of the argument to the help parameter. You can omit it. The passed value can be get with […]

  • 2020-03-04
  • 2020-03-04

How to create the disk image file which is able to mount on APFS unsupported OS

The default file system of the macOS is changed to APFS since macOS High Sierra 10.13. The file system of the disk image file which is created by the Disk Utility had been changed to APFS also. They are not able to mount on earlier OS than macOS 10.13 which not supports APFS. This article how to create the disk image file with specify the file system. Creating the disk image file To create the disk image file, you can following way: Use the Disk Utility Use the hdiutil To specify the file system, use hdiutil to create the disk image file. Creating the disk image file with hdiutil To create the disk image file with hdiutil, enter the following line in the Terminal. The first argument is an action. The create specify create the disk image file. The -srcfolder option specify the source folder which contains files and folders are copied into the disk image. Above example specify the Test folder in the current directory. The -fs option specify the file system. The HFS+ is a default system which had been used until macOS 10.12. The last argument is a disk image file. Above example creates the Test.dmg file. The file system you […]

  • 2020-03-03
  • 2020-03-03

How to create the zip same as Finder creates, from the terminal or the shell script

Finder has a function create the zip file from the file or the folder. This zip file is different from the zip file Windows creates. This article says this zip file as Finder style zip file. You can create this zip file from the terminal and the shell script. You can create it in the automated script. This article explains that how to create the Finder style zip file and how to expand it. Creating the Finder style zip file You can create the Finder style zip file with ditto. Above line create the file by archiving the Test folder. -c option specify creates the archive, -k option specify compress with PKZip, --sequesterRsrc option create the __MACOSX folder to store the resource fork and the HFS meta data. Specify only one file If you create the zip file with only one file, --keepParent option may be an obstacle. For example, following line create the zip file by archiving the 123.jpg file in the desktop. Expand it, the Desktop folder will be created and the 123.jpg file is put in the created folder. Redundant folder will be created. --keepParent option put the Desktop folder which is a parent folder of the […]