10行以下のテキストファイルを削除
def pre_osouji(path):
#10行で終わっているファイルを削除する。
delete_list=[]
for file in path:
with open( file, "r", encoding="cp932") as f:
lineral=f.read().splitlines()if len(lineral) < 15:
delete_list.append(file)for file in delete_list:
os.remove(file)
path.remove(file)
解説
with open - as f:
クローズしなくていい。インデント部分がOPEN範囲。
f.read().splitlines()
テキストを読み取って改行で区切って配列に格納。よく使う。
readlines()との違いがわからない。