txt内容,如:
/test/A/a.txt
/test/A/b.txt
/test/A/c.txt
/test/A/d.txt
/test/B/a.txt
/test/B/b.txt
/test/B/c.txt
/test/B/d.txt
/test/X/ABC
/test/temp.txt
。。。
需求:
1.把/test全部删除
2.找到/X/ABC,然后在后面加上一行/X/XYZ
3.找到/temp.txt以及空行,删除这几行
结果:
/A/a.txt
/A/b.txt
/A/c.txt
/A/d.txt
/B/a.txt
/B/b.txt
/B/c.txt
/B/d.txt
/X/ABC
/X/XYZ
。。。
这个代码运行之后不会把原来的inputfile.txt直接改掉的。而且也不会生成新的文件。
如果要生成新的文件的话该怎么改?
而且换行符要用Linux的LF,不是Windows的CR+LF
newfilename="xxxx"
open(newfilename,"wb").write("\n".join(outputs))
这样就可以了。
给个一行搞定的:
with open('inputfile.txt', 'r') as f, open('outputfile.txt', 'wb') as g: