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

发布于 2024-11-07 23:46:50 字数 596 浏览 0 评论 0原文

由于我不熟悉Python,所以我想我应该来这里。

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

脚本以相同的方式运行 'ie':sh script.sh

基本上我需要为每个文件夹运行这个脚本,问题是我不知道如何进入每个文件夹(有脚本),这是 4 个级别从原始文件夹下来。

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

最好的方法是什么?我想我可以弄清楚检查结果,但老实说我不太熟悉Python。

为了让大家清楚,文件夹结构是这样的:
顶级(20 个左右文件夹)
 --> 下面 1 个(大约 5 个文件夹)
 ----->这5个文件夹之一下面的1(仅包含1个文件夹)
 --------->1 在该文件夹下(这里是脚本所在的位置)

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

Since Im unfamiliar with Python I thought i'd come here.

Anyways I have several Folders (lets say 20 folders)
Within each folder is more folders (lets say 5 per)
and within EACH one of these folders is another folder, and within THAT is a script I need to run.

The script is ran the same way 'ie':sh script.sh

Basically I need to run this script for each of these folders, problem is I have no clue how to step into each folder (that has the script) which is 4 levels down from the original folder.

So there are a total of like 120 scripts that need to be ran the same way (this script also returns a number for instance (1 for success, 0 for failure) that I need to record (shouldn't be too hard to figure out)

What would be the best way to go about this? The Checking results I think I can figure out, but stepping through all these subfolders Im honestly not well versed in python to know about.

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)

Don't ask me why the folders are structured this way.....lol. This would be done in python 2 preferably.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

何时共饮酒 2024-11-14 23:46:50
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
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
怪我闹别瞎闹 2024-11-14 23:46:50

有点矫枉过正,但它应该有效:

import os

root = '/foo/'

for directory, subdirectories, files in os.walk(root):
  for file in files:
     if os.path.splitext(file)[-1].lower() == '.sh':
       os.system('sh ' + os.path.join(directory, file))

A bit overkill, but it should work:

import os

root = '/foo/'

for directory, subdirectories, files in os.walk(root):
  for file in files:
     if os.path.splitext(file)[-1].lower() == '.sh':
       os.system('sh ' + os.path.join(directory, file))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文