我想让 python-script 在目录中按模式搜索文件并显示结果。在 shell 中它很容易,并且是在一个小时内完成的。
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 中
import subprocess,os,datetime,fnmatch
path='/root/bkp'
date=datetime.date.today()
pattern=str('%s' %date)
def find_file():
obj=re.compile(pattern)
for root,dirs,files in os.walk(path):
for f in files:
match=obj.search(f)
if match:
print ‘File in ??? is OK’ ===== # need directory mention
else:
print ‘no file’
find_file()