gpt4 book ai didi

python - 使用正则表达式查找与 Python 中的模式匹配的所有行

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

<分区>

我有:

# runPath is the current path, commands is a list that's mostly irrelevant here
def ParseShellScripts(runPath, commands):
for i in range(len(commands)):
if commands[i].startswith('{shell}'):
# todo: add validation/logging for directory `sh` and that scripts actually exist
with open(os.path.join(runPath, 'sh', commands[i][7:]),"r") as shellFile:
for matches in re.findall("/^source.*.sh", shellFile):
print matches

但是,我得到这个错误:

Traceback (most recent call last):
File "veri.py", line 396, in <module>
main()
File "veri.py", line 351, in main
servList, labels, commands, expectedResponse = ParseConfig(relativeRunPath)
File "veri.py", line 279, in ParseConfig
commands = ParseShellScripts(runPath, commands)
File "veri.py", line 288, in ParseShellScripts
for matches in re.findall("/^source.*.sh", shellFile):
File "/usr/lib/python2.7/re.py", line 177, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer

编辑: 添加一些文件作为示例

#config.sh
#!/bin/bash

dbUser = 'user'
dbPass = 'pass'
dbSchema = ''
dbMaxCons = '4000'

#the shellFile I'm looking in
#!/bin/bash
source config.sh

OUTPUT=$(su - mysql -c "mysqladmin variables" | grep max_connections | awk '{print $4}')
if [[ ${OUTPUT} -ge ${dbMaxCons}]]; then
echo "Success"
echo ${OUTPUT}
else
echo ${OUTPUT}
fi

基本上我想要完成的是搜索 sh 目录中的所有指定文件,如果其中任何一个包含 source*.sh(例如,source config.sh), 打印那个文件(最终我会把它展开并将它附加到当前文件的顶部,这样我就可以通过 ssh 传递单个命令字符串..但这不相关在这里我不认为。)

我做错了什么?

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