运行 在终端中移动导演的脚本

Run a script that moves director in the terminal

在终端中,我可以简单地使用以下命令四处移动:

cd [file path]

但是我需要一个自动执行此操作的脚本,因为我需要进入一堆文件夹(大约 200 个)并且 运行 每个文件夹中都有一个程序。

到目前为止,我已经尝试过 shutil.move() 之类的操作,但它无法满足我的需要,因为它会移动我的代码所在的整个文件夹,包括我的 IDE,这会导致我的程序崩溃。

有什么方法可以 运行 python 中的脚本打开终端 shell,通过文件路径导航到文件夹,然后在该文件夹中执行程序文件夹?

为此,您可以使用 os 模块。然后这样做:

for dir in os.listdir():
    if os.path.isdir(dir):
        os.chdir(dir)
        # Then do whatever program you wish to run