简体   繁体   English

从批处理文件中调用 python 行

[英]calling a python line from a batch file

I see here that I can call a python script from a batch file.在这里看到我可以从批处理文件中调用 python 脚本。

What I want to do is calling a python line without using a script.我想做的是在不使用脚本的情况下调用 python 行。

This is an example of what i'm trying to do:这是我正在尝试做的一个例子:

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

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

Note: there is an easier way of doing what i'm trying to do in my example like here but this is not the question:)注意:有一种更简单的方法来做我在我的例子中尝试做的事情,但这不是问题:)

You can use the -c flag:您可以使用-c标志:

Specify the command to execute (see next section).指定要执行的命令(见下一节)。 This terminates the option list (following options are passed as arguments to the command).这将终止选项列表(以下选项作为 arguments 传递给命令)。

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

this will work, thanks to @E.Serra approach.这将起作用,这要归功于@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

OK, if you are just trying to run your python.exe then:好的,如果您只是想运行 python.exe,那么:

python python.exe 

If you want to run perl style oneliners then:如果你想运行 perl 风格的 oneliners 然后:

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

Spits:吐口水:

$> this\is\a\path $> 这个\是\一个\路径

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

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