gpt4 book ai didi

python - 尝试调用 os.system 时为 "Unexpected indent"

转载 作者:太空宇宙 更新时间:2023-11-04 11:00:27 24 4
gpt4 key购买 nike

非常简单的问题(可能),但我想因为我是 python 的新手,所以我不太确定 os.system 是如何工作的。

基本上我有一个 for 循环(感谢 SO 的一些帮助)遍历我的所有目录并运行一个 shell 脚本(比如 120 个不同的 shell 脚本)。

import os

root = '/foo/'

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

基本上我想做的是这个(如果它是在unix中)(忽略IP地址但每次都会有一个IP地址)sh run.sh 157.111.22.134 >> 日志文件.txt然后sh remove.sh 157.111.22.134

我试过这样的东西

#!/usr/bin/env python

import os

root = "~/users/me"
ip = '157.111.22.134'
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) + ' ' + ip + '>>' ' log.txt')
os.system('sh ' + 'remove.sh ' + ip)

但它提示第二个 os.system(说“意外缩进”)

我什至不确定我是否正确?想法?

最佳答案

您混淆了缩进(空格与制表符)。使用python -tt验证。

关于python - 尝试调用 os.system 时为 "Unexpected indent",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6047055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com