2 回答
TA贡献1821条经验 获得超4个赞
改用标志。
def depdelete(path):
for path, dirs, files in os.walk(path):
for f in files:
found = False # Add a boolean as flag, reset each iteration
if f.endswith('.exe'):
found = True # Set the flag so "No further..." will not be triggered
os.remove(os.path.join(path, f))
print('Dep Files have been deleted from' + path)
with open(completeName, 'a') as ddr:
ddr.write('Dep Files have been deleted from' + path + '. \n')
if not found: # check the flag
print('No Dep Files found in' + path)
with open(completeName, 'a') as ddr:
ddr.write('No Further Dep Files found in' + path + '. \n')
TA贡献1815条经验 获得超13个赞
这似乎只需要一点重组。
def depdelete(path):
wereThereAnyDepFiles = False
for path, dirs, files in os.walk(path):
for f in files:
if f.endswith('.exe'):
os.remove(os.path.join(path, f))
print('Dep Files have been deleted from' + path)
with open(completeName, 'a') as ddr:
ddr.write('Dep Files have been deleted from' + path + '. \n')
wereThereAnyDepFiles = True
if not wereThereAnyDepFiles:
print("No Dep files found in "+path)
下面,写入文件的内容表明您希望这表明检查已结束并且您找不到更多的.exe文件。使用这个假设,最好将语句放在if块之外,就像我在下面所做的那样。如果我误解了您的意图,将语句放在if块中应该可以实现您的需要。
with open(completeName, 'a') as ddr:
ddr.write('No Further Dep Files found in'+path+'. \n')
添加回答
举报