|  | 
 
| 
import os
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  name1 = str(input('请输入目录'))
 list1 = list(os.walk(name1))
 list2 = []
 lentgh1 =len(list1)
 for i in range(lentgh1):
 lentgh2 = len(list1[i])
 for j in range(1,lentgh2):
 for k in list1[i][j]:
 (x,y)=os.path.splitext(k)
 if y == '.mp4' or y == '.avi':
 name =name1 +'\ '+k + '\n'
 list2.append(name)
 f1 = open('mp4.txt','w')
 f1.writelines(list2)
 f1.close()
 
 如题
 我运行完就显示
 Traceback (most recent call last):
 File "D:/python/jichu/查找文件格式.py", line 15, in <module>
 f1.writelines(list2)
 UnicodeEncodeError: 'gbk' codec can't encode character '\xe2' in position 30: illegal multibyte sequence
 
 只有列表中的第一个字符串被写入
 
试试这样: 复制代码
import os
name1 = str(input('请输入目录'))
list1 = list(os.walk(name1))
list2 = []
lentgh1 =len(list1)
for i in range(lentgh1):
    lentgh2 = len(list1[i])
    for j in range(1,lentgh2):
        for k in list1[i][j]:
            (x,y)=os.path.splitext(k)
            if y == '.mp4' or y == '.avi':
                name =name1 +'\ '+k + '\n'
                list2.append(name)
f1 = open('mp4.txt','w', encoding='utf-8')
f1.writelines(list2)
f1.close()
 | 
 |