请帮我把shell脚本转换成python

2024-05-16 03:12:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python脚本在目录中按模式搜索文件并显示结果。 在壳牌,它很简单,一个小时就做好了。在

date=`date +%F`
path=/root/bkp
for i in $(ls $path)
do
str=`find $path/$i -name “*$date*.txt”`
if [$str]
    then
        echo “File in $i is OK”
    else
        echo “File in $i is not found”
fi
done

在Python中

^{pr2}$

Tags: 文件pathinecho目录脚本fordate
1条回答
网友
1楼 · 发布于 2024-05-16 03:12:48

我对这个问题有点困惑,但是如果你只是在寻找文件名中是否有模式,那么你已经很清楚了。在

编辑:递归地遍历每个目录

import os,datetime
path = "C:\\Temp"
date=datetime.date.today()
pattern=str('%s' %date)
filefound = False
def find_file(currpath):
    for dirname, dirnames, filenames in os.walk(currpath):
        for files in filenames:
            if pattern in files:
                print("File found in " + currpath)
                global filefound
                filefound = True
                return
        for directory in dirnames:
           find_file(path+"\\"+directory)
find_file(path)
if filefound == False:
    print("File containing " + pattern + " not found in " + path)

相关问题 更多 >