从批处理函数调用python行

2024-04-19 10:10:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我看到here我可以从批处理文件调用python脚本。你知道吗

我想做的是调用python行而不使用脚本。你知道吗

这是我想做的一个例子:

>> echo 'python.exe ''' "this/is/a/path".replace('/','\\')'''  '

将返回"this\\is\\a\\path"

注意:在我的示例中,有一种更简单的方法来做我想做的事情,比如here,但这不是问题:)


Tags: 文件path方法echo脚本示例hereis
3条回答

多亏了@E.Serra的方法,这一切都会成功的。你知道吗

echo print("this/is/a/path".replace("/","\\"))  > tmp_file && python tmp_file && rm tmp_file

>

C:\Python27>echo print("this/is/a/path".replace("/","\\"))  >tmp_python_File && python tmp_python_File && rm tmp_python_File
this\is\a\path

您可以使用-c标志:

Specify the command to execute (see next section). This terminates the option list (following options are passed as arguments to the command).

python -c '''print("this/is/a/path".replace("/","\\"))'''
> this\is\a\path

好吧,如果你只是试着运行你的python.exe然后:

python python.exe 

如果您想运行perl风格的oneliners,那么:

echo '''print("this/is/a/path".replace("/","\\"))'''  >tmp_python_File && python tmp_python_File && rm tmp_python_File

吐口水:

$>;这是一条路径

相关问题 更多 >