简体   繁体   English

对变量使用subprocess.call

[英]Using subprocess.call with variables

I'm creating a routine which I will setup as a scheduled task in Windows to automatically take a copy of our database and post it to a Web Server for downloads by our customers. 我正在创建一个例程,该例程将在Windows中设置为计划任务,以自动获取我们的数据库副本并将其发布到Web服务器以供客户下载。

I've gotten everything working using static strings in the arguments for the subprocess call which uses 7-zip to create a self-extracting archive. 在使用7-zip创建自解压存档的子流程调用的参数中,我已经使用静态字符串来完成所有工作。 However, when I tried to make the logic a little more agnostic by using os.path.join and os.path.dirname to dynamically determine argument values for the subprocess.call function, things stopped working. 但是,当我尝试通过使用os.path.join和os.path.dirname动态确定subprocess.call函数的参数值使逻辑更加不可知时,事情就停止了工作。

I can't figure out what's wrong because when I run it in the cmd console providing static strings as the arguments it works fine. 我无法弄清楚出了什么问题,因为当我在cmd控制台中运行它并提供静态字符串作为参数时,它可以正常工作。 Additionally, it works fine through Python when I use static strings. 另外,当我使用静态字符串时,它可以通过Python正常工作。 It's only when I introduce the variables and functions into the arguments to try and dynamically determine the argument values and no error codes or messages are provided. 仅当我将变量和函数引入自变量中以尝试并动态确定自变量值时,才提供错误代码或消息。

# Get the Database location
localGDB = os.path.join(os.path.dirname(__file__), r"Resources\MyDatabase.gdb")

# Wrap the local File Geodatabase into a self-extracting exe
print "Wrapping local database..."
try:
    subprocess.Popen([str(os.path.join(os.path.dirname(__file__), r"Resources\7z\7-Zip64\7z.exe")),
                    "a",
                    "-sfx",
                    str(os.path.join(os.path.dirname(__file__), "MyDatabase.exe")),
                    str(localGDB)])
except Exception as e:
    ctypes.windll.user32.MessageBoxA(0, "An error occured while wrapping the database." + e.message, "ERROR", 0 | 0x10)
    sys.exit(1)

如果在Windows上,我建议运行subprocess.check_output()

Thanks Everyone. 感谢大家。 After taking tdelaney's advice to check the paths, I found that the 7zip path was incorrect. 在听取了tdelaney的建议以检查路径后,我发现7zip路径不正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM