Drafts

@cm3 の草稿置場 / 少々Wikiっぽく使っているので中身は適宜追記修正されます。

Windows コマンドプロンプトから Python に変数を渡す

結論

  • スペースを含む変数の場合は " で囲う必要がある。
  • しかし、~(チルダ) を使って自分で設定した変数の "(ダブルクオート)は外せない
    • %変数の使い方に書かれているような特殊変数には効果あり。
    • だから、ちゃんと受け渡し先で解釈されるように試行錯誤。
  • " で囲った場合は \ は基本的には一つでOK
    • ただし、" 直前に \ を置く場合は \\ とする。
    • 例: set path="C:\hoge\fuga piyo\hogera\\"

以下テスト例

> set a="a a"
> python test.py %a% %a%

['test.py', 'a a', 'a a']

> python test.py %a %a
['test.py', 'a']

> set b="b\"
> python test.py %b% %b%
['test.py', 'b" b"']

> set c=c\
> python test.py %c% %c%
['test.py', 'c\\', 'c\\']

> set d=d\\
> python test.py %d% %d%
['test.py', 'd\\\\', 'd\\\\']

> set e=e e\\
> python test.py %e% %e%
['test.py', 'e', 'e\\\\', 'e', 'e\\\\']

> set f="f f\\"
> python test.py %f% %f%
['test.py', 'f f\\', 'f f\\']

> set g="g\\ g\\"
> python test.py %g% %g%
['test.py', 'g\\\\ g\\', 'g\\\\ g\\']

> set h="h\ h\\"
> python test.py %h% %h%
['test.py', 'h\\ h\\', 'h\\ h\\']

> python test.py %~h%
The following usage of the path operator in batch-parameter
substitution is invalid: %~h%

For valid formats type CALL /? or FOR /?

> set i="i i\"
> python test.py %i% %i%
['test.py', 'i i" i', 'i"']

test.py

import sys
print(sys.argv)