

如上图所示,判断文件(query_result.csv)内容,如果此文件里面出现了undefined这句话,就删除文件,若没有出现这句话则将文件放到桌面。这个功能用python如何实现?


关注
import os
import shutil
#桌面地址换成你自己的
def test_file(filepath,desk=r“C:\USER\Administrator\桌面”):
filepath = os.path.join(filepath)
desk = os.path.join(desk)
flag = False
with open(filepath,"r") as r:
for i in r:
if 'undefined' in i:
flag = True
break
if flag:
os.remove(filepath)
else:
shutil.move(filepath,desk)