python中writelines写入行的问题
本帖最后由 checkily 于 2018-2-13 11:04 编辑为什么我用writelines,把列表写入文件的时候,为什么不是分行写入的,要怎样改??
列表是:
输出结果是:
我的代码是:import os
def find_file(dirs):
os.chdir(dirs)
lis=[]
for each in os.listdir(os.curdir):
ext=os.path.splitext(each)
if ext in (".py",".txt"):
lis.append(os.getcwd()+os.sep+each)
if os.path.isdir(each):
find_file(each)
os.chdir(os.pardir)
return lis
def ext_file():
temp=find_file(dirs)
f=open("list.txt","w")
f.writelines(temp)
f.close()
dirs=input("input the dirs:")
ext_file()
参见http://blog.csdn.net/sxingming/article/details/51338114
那我明白了。我第10行写的是lis.append(os.getcwd+os.sep+each),少添加了一个换行符os.linesep。应该改为lis.append(os.getcwd()+os.sep+each+os.linesep)
可以了,谢谢
页:
[1]