Python の subprocess を使用して、Python スクリプト内で Powershell スクリプトを呼び出していますPopen
。Powershell スクリプトには、 と の 2 つの入力パラメーターが必要-FilePath
です-S3Key
。ファイルを AWS S3 サーバーにアップロードします。ハードコードされた文字列を渡すと、スクリプトは機能します。
os.Popen([r'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe','-ExecutionPolicy','RemoteSigned','./Upload.ps1 -FilePath \"C:\TEMP\test.txt\" -S3Key \"mytrialtest/test.txt\"'])
ただし、Python 文字列変数を渡そうとすると、Powershell スクリプトは、ファイル名変数で指定されたファイルが見つからないというエラーを出します。
filename = 'C:\TEMP\test.txt'
uploadkey = 'mytrialtest/test.txt'
os.Popen([r'C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\powershell.exe','-ExecutionPolicy','RemoteSigned','./Upload.ps1 -FilePath \"filename\" -S3Key \"uploadkey\"'])
どんな助けでも大歓迎です。ありがとう。