鱼C论坛

 找回密码
 立即注册
查看: 2278|回复: 7

[已解决]python代码求优化

[复制链接]
发表于 2021-9-14 10:59:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码目的是找出对应类型的文件,可结果里会有不相干的文件名,甚至有名字不相干的目录名


  1. import os

  2. def file_find(dir_position, suffix):
  3.     os.chdir(dir_position)
  4.     all_files = os.listdir(os.curdir)

  5.     for each_file in all_files:
  6.         if os.path.splitext(each_file)[1] in suffix:
  7.             viewlist.append(os.getcwd() + os.sep + each_file + '\n')
  8.         if os.path.isdir(each_file):
  9.             file_find(each_file, suffix)
  10.             os.chdir(os.pardir)

  11. dir_position = input('请输入路径:')
  12. suffix = input('请输入要查找的文件类型(.txt):')

  13. viewlist = []

  14. file_find(dir_position, suffix)

  15. f = open(dir_position + os.sep + 'viewlist.txt', 'w')
  16. f.writelines(viewlist)
  17. f.close()
复制代码



圈出来的是目录,而且名字都没有关系,为什么会打印出来
file:///C:/Users/123/Desktop/Inked%E6%8D%95%E8%8E%B7_LI.jpg
最佳答案
2021-9-14 12:12:28
  1. import os


  2. def file_find(dir_position, suffix):
  3.     os.chdir(dir_position)
  4.     all_files = os.listdir(os.curdir)

  5.     for each_file in all_files:
  6.         if os.path.isdir(each_file):
  7.             file_find(each_file, suffix)
  8.             os.chdir(os.pardir)
  9.         else:
  10.             if os.path.splitext(each_file)[1] in suffix:
  11.                 viewlist.append(os.getcwd() + os.sep + each_file + '\n')


  12. dir_position = input('请输入路径:')
  13. suffix = input('请输入要查找的文件类型(.txt):')

  14. viewlist = []

  15. file_find(dir_position, suffix)

  16. f = open(dir_position + os.sep + 'viewlist.txt', 'w', encoding="utf-8")
  17. f.writelines(viewlist)
  18. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-9-14 11:01:14 | 显示全部楼层
怎么发图片啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-14 12:04:20 | 显示全部楼层
新人发图三件套 官方教程 图壳 图桌
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-14 12:07:18 | 显示全部楼层
本帖最后由 blahblahfc 于 2021-9-14 12:12 编辑

可能是第8行:
  1.         if os.path.splitext(each_file)[1] in suffix:
复制代码

无后缀文件或文件夹时 os.path.splitext(each_file)[1] 返回空字符串 ''
导致 if 条件 '' in suffix 一直为真,suffix 的值是字符串

  1. os.path.splitext('foo')
  2. # 返回 ('foo', '')
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-14 12:12:28 | 显示全部楼层    本楼为最佳答案   
  1. import os


  2. def file_find(dir_position, suffix):
  3.     os.chdir(dir_position)
  4.     all_files = os.listdir(os.curdir)

  5.     for each_file in all_files:
  6.         if os.path.isdir(each_file):
  7.             file_find(each_file, suffix)
  8.             os.chdir(os.pardir)
  9.         else:
  10.             if os.path.splitext(each_file)[1] in suffix:
  11.                 viewlist.append(os.getcwd() + os.sep + each_file + '\n')


  12. dir_position = input('请输入路径:')
  13. suffix = input('请输入要查找的文件类型(.txt):')

  14. viewlist = []

  15. file_find(dir_position, suffix)

  16. f = open(dir_position + os.sep + 'viewlist.txt', 'w', encoding="utf-8")
  17. f.writelines(viewlist)
  18. f.close()
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-14 12:18:12 | 显示全部楼层
你那样的逻辑会一些问题,我也很难说清楚
一般我用的逻辑是
  1. if 是否为文件夹:
  2.     xxx
  3. else:
  4.     if 是否满足要求:
  5.         xxxx
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-9-14 12:19:30 | 显示全部楼层
大马强 发表于 2021-9-14 12:18
你那样的逻辑会一些问题,我也很难说清楚
一般我用的逻辑是

因为不是文件夹,就是一个文件,所以用if else 搭配就不用两次判断
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-9-15 09:52:22 | 显示全部楼层
大马强 发表于 2021-9-14 12:04
新人发图三件套 官方教程 图壳 图桌

感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 14:19

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表