简体   繁体   English

在目录中的所有文件上运行exe文件的Python脚本

[英]Python script to run an exe-file on all files in a directory

I have a small .exe file that I'd like to run on all files in a given directory. 我有一个小的.exe文件,希望在给定目录中的所有文件上运行。 It is launched as follows "./myprogram.exe file" and converts my files into a different data type. 它按以下“ ./myprogram.exe文件”启动,并将我的文件转换为其他数据类型。

Now I want to build a python script, that runs this exe on all files in a given directory, including all subdirectories. 现在,我想构建一个python脚本,在给定目录中的所有文件(包括所有子目录)上运行该exe。 I am totally new to python, so I have no clue how to do this. 我对python完全陌生,所以我不知道如何执行此操作。

Does anyone have suggestions how to do this? 有人建议如何做吗? Do I need python or any other script language in the first place? 首先我需要python还是其他脚本语言?

Thanks a lot! 非常感谢!

On a linux box, I would have told you to use a find call, but the .exe seems to tell you are on windows. 在Linux机器上,我会告诉您使用find调用,但是.exe似乎告诉您在Windows上。

In python, you should use something like this 在python中,您应该使用类似这样的内容

for root, dir, files in os.walk(path):
   for name in files:
      subprocess.call(["path_to_your_programm/myprogram.exe", os.path.join(root, name)]

For a match of all files from a directory + sub directories, take a look at: Use a Glob() to find files recursively in Python? 要匹配目录+子目录中的所有文件,请看一下: 使用Glob()在Python中递归查找文件?

To call your program, take a look at : How do I execute a program from python? 要调用您的程序,请看一下: 如何从python执行程序? os.system fails due to spaces in path 由于路径中的空格,os.system失败

You should be able to modify the example in the python docs for os.walk to do that. 您应该能够在python文档中修改os.walk的示例来执行此操作。

To actually call your external program, you can use subprocess.call . 要实际调用外部程序,可以使用subprocess.call

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

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