简体   繁体   English

如何从从python文件执行的vbscript返回字符串

[英]How to return a string from a vbscript which is executed from a python file

I am new to VBScript and am executing a vbs file from python file. 我是VBScript的新手,正在从python文件执行vbs文件。 Now in the vbs file there is a String value which I need to return back to the python file. 现在在vbs文件中有一个String value ,我需要返回到python文件。 I have tried with WScript.Quit()/ WScript.StdOut() . 我已经尝试过WScript.Quit()/ WScript.StdOut() But it seems they don't support Strings. 但似乎他们不支持字符串。 'coz there is a Type mismatch error. '因为存在类型不匹配错误。

Is there any other way to do that. 还有其他方法可以做到这一点。

If you run your .vbs in the console, ie using CScript.exe , the you can redirect the output using ">": 如果您在控制台中运行.vbs,即使用CScript.exe ,则可以使用“>”重定向输出:

CScript example.vbs //NoLogo > output.txt

And inside your .vbs use WScript.StdOut object with .Write or .WriteLine method. 而且里面你的.vbs使用WScript.StdOut对象与.Write.WriteLine方法。 For example: 例如:

' example.vbs
Main

Sub Main()
    Dim result
    result = 1 / Cos(25)
    WScript.StdOut.Write result
End Sub

But if you run your script with WScript.exe ... 但是,如果您使用WScript.exe运行脚本,则...

WScript example2.vbs

...you'll need to write your output to file using FileSystemObject . ...您需要使用FileSystemObject将输出写入文件。

' example2.vbs
Main

Sub Main()
    Dim result, fso, fs
    result = 1 / Cos(25)
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set fs  = fso.CreateTextFile("output.txt", True)
    fs.Write result
    fs.Close
End Sub

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

相关问题 如何从在Python中执行的R脚本返回多个对象 - How to return multiple objects from R script executed in Python 从 python 调用 vbscript - Call vbscript from python 如何从 go 到 python 返回错误字符串 - How to return an error string from go to python 已调用Python脚本,但未从批处理文件执行 - Python script is called but not executed from batch file Python 脚本没有从我的批处理文件中执行 - Python script is not executed from my batch file 如何从Python3中使用Python2编码的文件中检索UTF-8编码(来自unicode)字符串的正确值? - How to retrieve correct value of a UTF-8 encoded (from unicode) string from a file from Python3 which was encoded using Python2? 如何从执行的字符串中检索值? - How to retrieve a value from an executed string? 执行python脚本时,如何查找执行sudo的用户名? - When executing python script, How to find username from which sudo is executed? 在Python中,当执行`if`语句时,如何从函数返回值? - In Python, how do I return a value from a function when an `if` statement is executed? Python:如何仅从一个 function 获取返回值(使用 asyncio.gather 执行的几个) - Python: How to obtain return value from only one function (out of several executed with asyncio.gather)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM