Scripting 技巧: 濫用python直譯器

前陣子因為需要在windows上寫比較複雜的script,又不想學powershell,靈光一閃想到可以濫用python直譯器來做事

python 有個-c選項:

-c cmd : program passed in as string (terminates option list)

然後就可以

python -c "import datetime; print(datetime.datetime.now().strftime(\"%Y/%m/%d\"))"
> 2025/07/21

就可以在shell/batch/powershell裡面直接原地呼叫python module來做事
也不用作參數處理了,直接用字串帶進去python就好

留言