python读取文件指定内容生成TXT的问题。 20

从网上扒了些代码拼在一起然后自己改了改,但是不知道为啥输出的文件里只有前两个文件的内容,看了错误提示还是不知道问题在哪。Traceback(mostrecentcalll... 从网上扒了些代码拼在一起然后自己改了改,但是不知道为啥输出的文件里只有前两个文件的内容,看了错误提示还是不知道问题在哪。
Traceback (most recent call last):
File "C:\Users\38091\Desktop\sis.py", line 25, in <module>
get_col_by_index(txt_path)
File "C:\Users\38091\Desktop\sis.py", line 17, in get_col_by_index
website=l.split()[1]
IndexError: list index out of range
这是代码
import os
imgfiles = []
for path, dirs, files in os.walk("C:\\7.2030"):
imgfiles.extend([os.path.join(path, f) for f in files if f.endswith('.txt')])

def get_col_by_index(txt_path):
if not txt_path:
print ('the index or the file is null ,pelase check it ')
return

if not os.path.isfile(txt_path):
print ('file does the exists in the path: %s ' % txt_path)
return

f = open(txt_path,'r')
for l in f.readlines():
website=l.split()[1]
output=open ('data1.txt','a')
output.write(website+'\n')
output.close()
pass

for txt_path in imgfiles:
get_col_by_index(txt_path)
pass
展开
 我来答
月轮天唯五
2016-08-11 · TA获得超过377个赞
知道小有建树答主
回答量:387
采纳率:100%
帮助的人:156万
展开全部
website=l.split()[0] 改成0试试
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tolerious
2016-08-01 · 超过16用户采纳过TA的回答
知道答主
回答量:77
采纳率:0%
帮助的人:17万
展开全部
website = l.split()[1] 这句话没问题么?
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式