从test.txt文本中逐行读取字符串,以字符串命名,生成新的txt文档
#-*- coding:UTF-8 -*-
#读取字符串,去除字符串中的“\n”,并生成txt文件
if __name__=='__main__':
sp = open('test.txt','r')
for i in sp:
if "\n" in i:
t = i.replace('\n','')
filename = t + ".txt"
fp = open(filename,'w')
fp.write('xyz')
fp = open(filename,'r')
print fp.read()
fp.close()
运行结果:
>>>
xyz
xyz
xyz
xyz
>>>
test.txt文本内容