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 展开
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 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询