简体   繁体   English

从 windows 批处理文件运行 python 脚本 - 文件名冲突

[英]Running python script from windows batch file - filename conflicts

I have a python script script.py designed to run at the command line.我有一个 python 脚本script.py设计为在命令行运行。 To make it easier to run, I have a windows batch file script.bat which sets up some defaults and other things.为了更容易运行,我有一个 windows 批处理文件script.bat ,它设置了一些默认值和其他内容。 They exist in the same directory.它们存在于同一目录中。

If I run >script at the command prompt, then the python script is run preferentially to the batch file.如果我在命令提示符下运行>script ,则 python 脚本优先运行到批处理文件。 >script.bat works as expected. >script.bat按预期工作。

>where script lists the batch file first, so to my understanding, it should be run preferentially to the python script. >where script首先列出批处理文件,所以据我了解,它应该优先运行到 python 脚本。

Can I ensure that the batch file is run preferentially without renaming or using the file extension?我可以确保批处理文件优先运行而不重命名或使用文件扩展名吗?

the order of where is not the order it will execute. where的顺序不是它将执行的顺序。 Where lists files in the local path first, in alphabetic order, then will it list the names in the environment path. where 首先按字母顺序列出本地路径中的文件,然后会列出环境路径中的名称。 So assume in the working dir you have files:所以假设在工作dir中你有文件:

test
test.bat
test.cmd
test.py

that is how they will be listed, alphabetically.这就是它们将按字母顺序列出的方式。

which executes first is a matter of order from the pathext variable, an example, by running set pathext from cmd :首先执行的是pathext变量的顺序问题,例如,通过从cmd运行set pathext pathext :

PATHEXT=.PY;.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PYW

In this modified version, I placed .py first, meaning that if only the name is specified it run by the extension in the order of the list.在这个修改后的版本中,我将.py放在首位,这意味着如果只指定名称,它就会由扩展程序按照列表的顺序运行。 So given your example of only the 2 similar files with extensions .bat and .py here script.py will be launched first.. So if I move it to the end:因此,鉴于您的示例仅包含扩展名为.bat.py的 2 个类似文件,这里script.py将首先启动。所以如果我将它移到最后:

PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY;.PYW

Now if we run script will it launch the .bat first.现在,如果我们运行script ,它会首先启动.bat

Solved by making my script into a zip file containing a main .py.通过将我的脚本制作成包含.py 的 zip 文件来解决。 script.pyz runs after the batch. script.pyz在批处理之后运行。

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

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