我有多个.txt文件(O1test,O2test,O3test,O4test)
text of 01test(hello,my,friend,sorry,newbie)
text of 02test(hello,my,friend,sorry,newbie)
text of 03test(hello,my,friend,sorry,noob)
text of 04test(hello,my,friend,sorry,amatuer)
输出txt文件可能如下所示
Newbie
01test
02test
Noob
O3test
Amatuer
O4test
正如你所看到的,我需要写一个文件名和第xx行文字,再写一次文件名和第xx行文字,但如果第xx行与前一行相同,则继续下一行
我试过一些东西,但现在我卡住了
import os
for root, dirs, files in os.walk("C:\\Users\\42077\\Desktop\\test\\"):
for file in files:
if file.endswith(".txt"):
with open("C:\\Users\\42077\\Desktop\\test\\!output.txt", "a") as f: as f
f.write(os.path.join( file)+"\n")
如何阅读第五行:打开文件,在数数行的同时在行上循环;到第五站时停车
如果文件的行数较少,该怎么办?我决定提出一个错误,但不同的设计肯定是可能的
要组织一组值的结果以及它们所在的文件,请使用
dict
列表在结果
dict
上循环并打印结果应该相当明显;我把这个留作练习相关问题 更多 >
编程相关推荐