遍历一个文本文件word.txt,将长度超过10的单词写入当前路径下的的一个新文件是什么意思?如何实现,
f_old=open('word.txt', 'r')
f_new=open('new_word.txt', 'a')
for i in f_old.readlines():
if len(i) >= 10:
f_new.write(i)
f_new.write('\n')
f_new.close()
f_old.close()
如有帮助请采纳
望采纳,谢谢!
with open('word.txt', 'r') as f:
with open('save.txt', 'a') as f2:
for one in f.readlines():
if len(one) >= 10:
f2.write(one + '\n')
with open(r"d:\file1.txt", "r") as f:
data = f.read()
words = [w for w in data.split('\n') if len(w)>10]
with open(r"d:\file2.txt", "a") as f:
f.writelines('\n'.join(words))