在 VBS 中使用 WshShell.Run 运行 Python 脚本不会生成输出文件 [英] Running Python script using WshShell.Run in VBS does not generate output file

查看:55
本文介绍了在 VBS 中使用 WshShell.Run 运行 Python 脚本不会生成输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写一些 VB 脚本时遇到了问题 - 解决起来似乎并不难,但即使在谷歌的许多页面上跋涉之后,我仍未找到解决方案.

I've got a problem with some VB scripting - it doesn't seem like it should be terribly difficult to solve, but even after trudging through many a page of Google I have yet to find a solution.

这是我的python文件(test.py),简化为仅显示问题:

Here is my python file (test.py), simplified to just show the problem:

f = open("testing.txt", 'w')
f.write("oh hai\n")
f.close()

当然,当直接从命令行运行时,这会按照您的预期生成文件.

Of course, when run directly from the command line, this generates the file as you'd expect.

然而,当在一个简单的 .vbs 脚本中运行时(警告:我的 vbs 技能缺乏.这可能是我遇到问题的原因.到目前为止,我没有遇到很多问题,除了讨厌使用 XP 的生活习惯使用vim时的代码)

However, when run in a simple .vbs script (WARNING: My vbs skills are lacking. This is probably why I am having a problem. So far I haven't had many issues, apart from hating life from using XP to code when I'm used to using vim)

Set WshShell = WScript.CreateObject("WScript.Shell")
cmd = "C:\Python27\python test.py"
WshShell.Run cmd

没有生成输出文件!根本!真令人气愤,因为当我从开始菜单将那个确切的命令 ("C:\Python27\python test.py") 输入到运行程序时,它起作用了!

no output file is generated! At all! It's infuriating, as when I input that exact command ("C:\Python27\python test.py") into the run program from the start menu, it works!

在工作中,所以他们使用的是 Windows XP.其他一切都非常标准,或者我相信.

At work, so they're on Windows XP. Everything else is pretty standard, or so I'm lead to believe.

将C:\Python27\testing.py"更改为testing.py".这是我试图解决它时遗留下来的,并认为可能是将文件放在目标文件夹之外的某个地方.

Changed "C:\Python27\testing.py" to just "testing.py". This was left over from when I was trying to solve it, and thought maybe it was putting the files somewhere outside of the destination folder.

推荐答案

首先,您的 Python 脚本看起来很可疑,我怀疑反斜杠在简单字符串中是否有效.至少,在我的测试中,它不起作用,我只是将它们替换为正斜杠.

First, your Python script looks suspicious, I doubt the backslashes work in a simple string. At least, in my test, it didn't work, I just replaced them with forward slashes.

接下来,您可以通过在 cmd 前面加上 cmd/k 来查看错误消息,运行窗口将保留在屏幕上.您可以看到 .py 文件未找到,因为它不在当前目录中.您必须指定此文件的绝对路径,可能是通过从脚本中获取当前路径.

Next, you can see the error message by prepending cmd with cmd /k, the run window remains on screen. You can see the .py file isn't found, because it isn't in the current directory. You have to specify an absolute path to this file, perhaps by getting the current path from the script.

我终于得到了一个工作代码(我的 VBS 有点生疏...)

I finally got a working code (my VBS is a bit rusty...)

Dim wshShell, fso, loc, cmd

Set fso = CreateObject("Scripting.FileSystemObject")
loc = fso.GetAbsolutePathName(".")
WScript.Echo loc

'~ cmd = "%ComSpec% /k C:\Languages\Python\python.exe " + loc + "\test.py"
cmd = "C:\Languages\Python\python.exe " + loc + "\test.py"
WScript.Echo cmd

Set wshShell = CreateObject("WScript.Shell")
wshShell.Run cmd

如果提供了路径,您还可以检查参数:

You can also check the arguments if a path is provided:

if WScript.Arguments.Count = 0 then
    loc = fso.GetAbsolutePathName(".")
else
    loc = WScript.Arguments(0)
end if

这样的脚本最好使用 cscript 而不是默认的 wscript.

Such script is better run with cscript instead of default wscript.

这篇关于在 VBS 中使用 WshShell.Run 运行 Python 脚本不会生成输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆