怎么使用python读取txt文件的内容

这篇文章主要介绍怎么使用python读取txt文件的内容,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

python有哪些常用库

python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

注意,本文代码是使用在txt文档上,同时txt文档中的内容每一行代表的是图片的名字。

#coding:utf-8 
import shutil 
readDir = "原文件绝对路经" 
writeDir = "写入文件的绝对路径" 
#txtDir = "/home/fuxueping/Desktop/1" 
lines_seen = set() 
outfile=open(writeDir,"w") 
f = open(readDir,"r") 
for line in f: 
  if line not in lines_seen: 
    outfile.write(line) 
    lines_seen.add(line) 
outfile.close() 
print "success"

最终结果在在写入文件内容中,没有重复内容。

以上是“怎么使用python读取txt文件的内容”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!