Python error detection run from Powershell

Thing you want to do

--Python error detection executed from Powershell --As an example, execute the Python function "Convert csv file delimiter to tab delimiter" --The encoding of csv to be read is utf-8.

point

--use try-except --Use exception handling except UnicodeDecodeError to detect deode error in python function --Receive the above error in Powershell

Sample code

--Code that calls the function for tab conversion

exeConvertCsvtoTsv.py



from defConvert import CsvToTsv
import sys

#Target file path
path = sys.argv[1]

#Convert from csv to tsv
CsvToTsv(path)   

--Tab conversion function

convertTav.py


import csv
import sys
def CsvToTsv(path):

    #Array for reading
    line = []
    try:
        #Read ⇒ The encoding of csv to read is utf-8 so an error occurs here
        with open(path, "r", newline="", encoding="cp932") as f:       
            #Create read object (comma separated)
            reader = csv.reader(f, delimiter = ",")
            #Read
            line = [row for row in reader]

        #writing
        with open(path, "w", newline="", encoding="utf_8") as f:
            #Create write object (tab delimited)
            writer = csv.writer(f, delimiter = "\t")
            #Write all together
            writer.writerows(line)

    #decode error
    except UnicodeDecodeError as err:
        print(err)
        sys.exit()

    finally:
        #close
        f.close

--Powershell code that executes python functions

main.ps1


#CSV to read
$csv = "test.csv"

try{ 
    #File confirmation
    if((Test-Path $out) -eq $false){
        $ret = "No File"
        Throw $ret
    }

    #Convert from CSV to TSV
    $ret = $null
    $ret = python exeConvertCsvtoTsv.py $out #$Receive the execution result on the python side with ret
    if($null -ne $ret){
        Throw $ret
    }
}
catch{
    Write-Host "SQL Execution Error`r`n$ret"
}
finally{
    Write-Host "end"
}

result


#decode error
SQL Execution Error
'cp932' codec can't decode byte 0x85 in position 7: illegal multibyte sequence
end

--Python is convenient because exception types are prepared for each type of exception. --except exception type as alias: `` ` --If it is a decode error UnicodeDecodeError --0 If a division by zero error, `` `ZeroDivisionError` `` --If you do nothing when an exception occurs, you can pass through with `` `pass

Recommended Posts

Python error detection run from Powershell
Run illustrator script from python
Run Aprili from Python with Orange
Run Python scripts synchronously from C #
Run Ansible from Python using API
Run Python Scripts from Cisco Memorandum_EEM
sql from python
Python Error Handling
MeCab from Python
Run a Python script from a C # GUI application
Run a Python file from html using Django
Run a python script from excel (using xlwings)
Use thingsspeak from python
Run Python from Excel VBA with xlwings & tutorial supplement
Run Python with VBA
Operate Filemaker from Python
Use fluentd from python
Run prepDE.py with python3
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Python Not Implemented Error
Today's python error: killed
Python from or import
Use MySQL from Python
Install python from source
Execute command from Python
Run Blender with python
Operate neutron from Python!
Python error list (Japanese)
Use MySQL from Python
Operate LXC from Python
Manipulate riak from python
Force Python from Fortran
Execute Python function from Powershell (how to pass arguments)
Use BigQuery from python.
Run BigQuery from Lambda
Execute command from python
Cloud Run tutorial (python)
[Python] Read From Stdin
Run iphone safari from mac with python + selenium + safari-webdriver
[Python] for statement error
Use mecab-ipadic-neologd from python
Run iperf with python
Import error resolution record when touching Python code from VSCode
How to run a Python program from within a shell script
Flatten using Python yield from
Call CPLEX from Python (DO cplex)
Deep Python learned from DEAP
Run Openpose on Python (Windows)
Post from Python to Slack
Run python with PyCharm (Windows)
Grammar features added from Python3.6
Cheating from PHP to Python
Run Python with CloudFlash (arm926ej-s)
Run automatic jobs in python
Let's run Excel with Python
Make MeCab available from Python3
Run shell commands in python
Run Python unittests in parallel
Information obtained from tweet_id (Python)
OCR from PDF in Python