030讲动动手3 文件保存问题
能不能帮忙看下为什么保存的文件是全部显示在一行的呀import os
def search_save(filelocation):
os.chdir(filelocation)
filename = os.listdir()
for each_file in filename:
if os.path.isdir(each_file):
search_save(each_file)
os.chdir(os.pardir)
else:
filetype = os.path.splitext(each_file)[-1]
if filetype in alltype:
filelist.append(os.getcwd() + os.sep + each_file)
filelocation = input('请输入待查找的初始目录:')
filelist = []
os.chdir(filelocation)
alltype = ['.mp4','.rmvb', '.avi']
search_save(filelocation)
f = open('videolist.txt','w')
f.writelines(filelist)
f.close()
字符串没有换行符吧,加上 '\n' 后就好了 filelist.append(os.getcwd() + os.sep + each_file + '\n')
import os
def search_save(filelocation):
os.chdir(filelocation)
filename = os.listdir()
for each_file in filename:
if os.path.isdir(each_file):
search_save(each_file)
os.chdir(os.pardir)
else:
filetype = os.path.splitext(each_file)[-1]
if filetype in alltype:
filelist.append(os.getcwd() + os.sep + each_file + '\n')
filelocation = input('请输入待查找的初始目录:')
filelist = []
os.chdir(filelocation)
alltype = ['.mp4', '.rmvb', '.avi']
search_save(filelocation)
f = open('videolist.txt', 'w')
f.writelines(filelist)
f.close() Twilight6 发表于 2020-7-14 15:24
字符串没有换行符吧,加上 '\n' 后就好了 filelist.append(os.getcwd() + os.sep + each_file + '\n')
...
没仔细看答案,加了+ os.linesep
页:
[1]