Arcticfoxer 发表于 2020-12-22 15:10:38

30讲,查找文件中视频格式的文件,问题

print('===视频查询程序===')
import os

def finvid(pth):
    os.chdir(pth)
    str1 = ''

    for each_file in os.listdir(os.curdir):
      name,ext = os.path.splitext(each_file)
      if ext == '.mp4' or '.rmvb' or '.avi':
            str1 += (os.getcwd() + os.sep + each_file + '\n\n')
      if os.path.isdir(each_file):
            finvid(each_file)
            os.chdir(os.pardir)
    a = open('D:\\vedioList.txt','w',encoding='utf-8')
    a.write(str1)
    a.close()


pth = input('请输入待查找的初始目录:')
finvid(pth)


如代码,运行后txt文件中写进去的是文件夹中的每个文件的路径,并没有查找视频文件

逃兵 发表于 2020-12-22 15:22:08

print('===视频查询程序===')
import os

def finvid(pth):
    os.chdir(pth)
    str1 = ''

    for each_file in os.listdir(os.curdir):
      name,ext = os.path.splitext(each_file)
      if ext == '.mp4' or ext == '.rmvb' or ext == '.avi':
            str1 += (os.getcwd() + os.sep + each_file + '\n\n')
      if os.path.isdir(each_file):
            finvid(each_file)
            os.chdir(os.pardir)
    a = open('D:\\vedioList.txt','w',encoding='utf-8')
    a.write(str1)
    a.close()


pth = input('请输入待查找的初始目录:')
finvid(pth)

Arcticfoxer 发表于 2020-12-22 17:07:27

逃兵 发表于 2020-12-22 15:22


再问一句,为何这样不行,只找到MP4
ifext == ('.mp4' or '.rmvb' or '.avi'):

逃兵 发表于 2020-12-22 17:20:26

Arcticfoxer 发表于 2020-12-22 17:07
再问一句,为何这样不行,只找到MP4
ifext == ('.mp4' or '.rmvb' or '.avi'):

可以改成
if ext in ('.mp4' , '.rmvb' , '.avi'):
==是判断是否相等,左边是字符串,右边是元组,肯定不相等

Arcticfoxer 发表于 2020-12-22 17:26:54

逃兵 发表于 2020-12-22 17:20
可以改成

==是判断是否相等,左边是字符串,右边是元组,肯定不相等

哦哦,感觉自己瓦特了{:10_277:}
页: [1]
查看完整版本: 30讲,查找文件中视频格式的文件,问题