Python,单步执行文件夹(以运行脚本)

标签 python python-2.6

因为我不熟悉Python,所以我想我应该来这里。

无论如何,我有几个文件夹(假设有 20 个文件夹) 每个文件夹内有更多文件夹(假设每个文件夹有 5 个) 每个文件夹中都有另一个文件夹,其中有一个我需要运行的脚本。

脚本的运行方式与 'ie':sh script.sh

基本上,我需要为每个文件夹运行这个脚本,问题是我不知道如何进入每个文件夹(包含脚本),该文件夹比原始文件夹低 4 层。

所以总共有大约 120 个脚本需要以相同的方式运行(这个脚本还返回一个数字,例如(1 表示成功,0 表示失败),我需要记录(不应该太难)弄清楚)

解决这个问题的最佳方法是什么?我想我可以弄清楚检查结果,但是逐步浏览所有这些子文件夹,老实说我不太熟悉 python 。

Just so everyones clear the folder structure is like this:
Top Level (20 or so folders)
 -->1 Below (5 or so Folders)
 ----->1 below one of these 5 folders (only 1 folder is contained)
 --------->1 below that one folder (here is where the script resides)

不要问我为什么文件夹是这样构造的......哈哈。这最好在 python 2 中完成。

最佳答案

import glob,subprocess
for script in glob.glob('*/*/*/*.sh'):
   s = subprocess.Popen([script], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
   stdoutdata,stderrdata = s.communicate()
   returncode = s.returncode
   # Do something with stdoutdata, returncode etc. here

关于Python,单步执行文件夹(以运行脚本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033441/

相关文章:

Python:以指定格式打印列表列表

python - datetime.timedelta 返回意外结果?

python - CentOS 在随机时间运行 python 脚本

python - 更改 SSL 协议(protocol) Torndao 2.3/Python 2.6

python - 属性错误: 'ElasticLoadBalancingv2' object has no attribute 'set_desired_capacity'

python - ipython 提示 readline

python - 有条件内存?

Python os.stat(file_name).st_size 与 os.path.getsize(file_name)

python - 重击 : pip: command not found

opencv - 通过Python程序捕获图像时出错