python 读取txt 和 写入 txt

本人喜欢这两种方式:

写入:

data_list=list([first_day,second_day,third_day,forth_day,fifth_day])
path=os.getcwd()
count=1
for item in data_list:
    fobj=open(path+'/'+str(count)+'.txt','w')
    fobj.writelines(['%s%s'%(eachline,os.linesep) for eachline in item])
    fobj.close()
    count+=1

读取:

content=[]
fobj=open(path+'/'+str(1)+'.txt','r')
for eachline in fobj:
   content.append(eachline.strip())


将一个list存入txt中:

fobj=open("c:\users\dell\desktop\content.txt",'w')
fobj.writelines(('%s%s'%(str(items),os.linesep) for items in content))#content是要保存的list。


你可能感兴趣的:(python,Python学习)