Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在Python2.7中的Powershell中运行一行?_Python 2.7_Powershell_Subprocess - Fatal编程技术网

Python 2.7 如何在Python2.7中的Powershell中运行一行?

Python 2.7 如何在Python2.7中的Powershell中运行一行?,python-2.7,powershell,subprocess,Python 2.7,Powershell,Subprocess,我有一行Powershell脚本,当我在Powershell的命令行中输入它时,它可以正常运行。在我从Powershell运行的Python应用程序中,我试图将这行脚本发送到Powershell powershell -command ' & {. ./uploadImageToBigcommerce.ps1; Process-Image '765377' '.jpg' 'C:\Images' 'W:\product_images\import'}' 我知道该脚本可以工作,因为我已经能

我有一行Powershell脚本,当我在Powershell的命令行中输入它时,它可以正常运行。在我从Powershell运行的Python应用程序中,我试图将这行脚本发送到Powershell

powershell -command ' & {. ./uploadImageToBigcommerce.ps1; Process-Image '765377' '.jpg' 'C:\Images' 'W:\product_images\import'}'
我知道该脚本可以工作,因为我已经能够从Powershell命令行自己实现它。然而,我无法让Python将这一行发送到shell,而不获得“非零退出状态1”

有人有什么想法可以帮忙吗?我试过:

  • 子进程检查调用()
  • subprocess.call()
  • subprocess.Popen()

也许这只是一个语法问题,但我还没有找到足够的文档来证实这一点。

在单引号字符串中使用单引号会破坏字符串。在外部使用双引号,内部使用单引号,反之亦然,以避免出现这种情况。本声明:

powershell-command'&{../uploadImageToBigcommerce.ps1;处理映像'765377'。jpg''C:\Images''W:\product\u Images\import'}
应该是这样的:

powershell-command“&{../uploadImageToBigcommerce.ps1;处理映像“765377”“。jpg”“C:\Images”“W:\product\u Images\import'}”
另外,我会使用
subprocess.call
(和一个报价函数),如下所示:

导入子流程
产品='765377'
scriptPath='./uploadImageToBigcommerce.ps1'
def qq(s):
返回“'%s'”%s
def process_映像(sku、文件类型、搜索目录、目标路径、脚本路径):
psCode='.'+脚本路径+';处理映像“+qq(文件类型)+”\
qq(搜索目录)+''+qq(目标路径)
subprocess.call(['powershell','-Command','&{'+psCode+'}',shell=True)
进程映像(产品,'.jpg',C:\Images',C:\webDAV',脚本路径)

在单引号字符串中使用单引号会断开字符串。在外部使用双引号,内部使用单引号,反之亦然,以避免出现这种情况。本声明:

powershell-command'&{../uploadImageToBigcommerce.ps1;处理映像'765377'。jpg''C:\Images''W:\product\u Images\import'}
应该是这样的:

powershell-command“&{../uploadImageToBigcommerce.ps1;处理映像“765377”“。jpg”“C:\Images”“W:\product\u Images\import'}”
另外,我会使用
subprocess.call
(和一个报价函数),如下所示:

导入子流程
产品='765377'
scriptPath='./uploadImageToBigcommerce.ps1'
def qq(s):
返回“'%s'”%s
def process_映像(sku、文件类型、搜索目录、目标路径、脚本路径):
psCode='.'+脚本路径+';处理映像“+qq(文件类型)+”\
qq(搜索目录)+''+qq(目标路径)
subprocess.call(['powershell','-Command','&{'+psCode+'}',shell=True)
进程映像(产品,'.jpg',C:\Images',C:\webDAV',脚本路径)

我打赌你的引用/引用转义在某个地方出错了-打印(行)的输出是什么?打印(行)输出:powershell-command'&{../uploadImageToBigcommerce.ps1;进程映像“765377”。jpg“C:\Images”“C:\webDAV”}我怀疑原来的powershell命令。它具有嵌套在单引号字符串中的单引号字符串。我并不惊讶它不能从Python中工作——我很惊讶它能工作<代码>powershell-命令“&{../uploadImageToBigcommerce.ps1;处理图像”765377“。jpg“命令内容从
”开始{
并在
结束-Image'
进程映像
在您的计算机上有效的PowerShell cmdlet吗?还是
uploadImageToBigcommerce.ps1
的一个参数,如果前者可能不在您的路径上,如果后者,则该分号将破坏一切。@TessellingEckler,那么我将如何生成
PowerShell-命令“&{../uploadImageToBigcommerce.ps1;处理图像'765377'。jpg''C:\Images''W:\product\u Images\import'}”
到一个包含所有引号的字符串中?我打赌您的引号/引号转义在某个地方是错误的。
打印(行)
的输出是什么?打印(行)输出:powershell-command”{../uploadImageToBigcommerce.ps1;进程映像“765377”“。jpg”“C:\Images”“C:\webDAV'}'我怀疑原来的PowerShell命令。它有单引号字符串嵌套在单引号字符串中。我不奇怪它在Python中不起作用-我很惊讶它能起作用。
PowerShell-command'&{../uploadImageToBigcommerce.ps1;处理映像“765377”“。jpg'
命令内容从
开始(&C){
并在
结束-Image'
进程映像
在您的计算机上有效的PowerShell cmdlet吗?还是
uploadImageToBigcommerce.ps1
的一个参数,如果前者可能不在您的路径上,如果后者,则该分号将破坏一切。@TessellingEckler,那么我将如何生成
PowerShell-命令“&{../uploadImageToBigcommerce.ps1;处理图像'765377'。jpg''C:\Images''W:\product\u Images\import'}”
到一个包含所有引号的字符串中?我打赌您的引号/引号转义在某个地方是错误的。
打印(行)
的输出是什么?打印(行)输出:powershell-command”{../uploadImageToBigcommerce.ps1;进程映像“765377”“。jpg”“C:\Images”“C:\webDAV'}'我怀疑原来的PowerShell命令。它有单引号字符串嵌套在单引号字符串中。我不奇怪它在Python中不起作用-我很惊讶它能起作用。
PowerShell-command'&{../uploadImageToBigcommerce.ps1;处理映像“765377”“。jpg'
命令内容从
开始(&C){
并在
结束-Image'
进程映像
在您的计算机上有效的PowerShell cmdlet吗?还是
uploadImageToBigcommerce.ps1
的一个参数,如果前者可能不在您的路径上,如果后者,则该分号将破坏一切。@TessellingEckler,那么我将如何生成
PowerShell-命令 " & {. ./
import subprocess

product = "765377"
scriptPath = "./uploadImageToBigcommerce.ps1"

def process_image(sku, fileType, searchDirectory, destinationPath, scriptPath):
    psArgs = "Process-Image '"+sku+"' '"+fileType+"' '"+searchDirectory+"' '"+destinationPath+"'"
    subprocess.check_call([create_PSscript_call(scriptPath, psArgs)], shell=True)

def create_PSscript_call(scriptPath, args):
    line = "powershell -command ' & {. "+scriptPath+"; "+args+"}'"
    print(line)
    return line

process_image(product, ".jpg", "C:\Images", "C:\webDAV", scriptPath)