鱼C论坛

 找回密码
 立即注册
查看: 1782|回复: 0

[技术交流] 零基础入门学Python 第 30 讲 文件系统:介绍一个高大上的东西

[复制链接]
发表于 2018-3-24 21:48:27 | 显示全部楼层 |阅读模式

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

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

x
注意:
1.需要用到字典,字典的用法需要多复习
2.需要用到递归,递归的用法要积累

  1. 动动手:
  2. 0.
  3. import os

  4. file_name = os.listdir('/Users/yufan/Documents/python编程/30/doc')
  5. print(file_name)

  6. txt = 0
  7. png = 0
  8. py = 0
  9. docx = 0
  10. doc = 0

  11. for each_file in file_name:
  12.     if '.txt' in each_file:
  13.         txt += 1
  14.     elif '.png' in each_file:
  15.         png += 1
  16.     elif '.py' in each_file:
  17.         py += 1
  18.     elif '.docx' in each_file:
  19.         docx += 1
  20.     elif '.' not in each_file:
  21.         doc += 1

  22. print('txt 是 %d, png 是 %d, py 是 %d, docx 是 %d, doc 是 %d' % (txt,png,py,docx,doc))

  23. 1.
  24. import os
  25. doc_path = '/Users/yufan/Documents/python编程/30'
  26. file_name = os.listdir(doc_path)

  27. file_path = []

  28. for each_file_name in file_name:
  29.     file_path.append(os.path.join(doc_path,each_file_name))

  30. for each_file in file_path:
  31.     print('文件 %s 的大小是 %d byte.' % (os.path.basename(each_file),os.path.getsize(each_file)))

  32. 2.
  33. import os

  34. doc_path = input('请输入待查找的初始目录:')
  35. while not os.path.isdir(doc_path):
  36.     doc_path = input('没有这个路径,请重新输(输入exit退出):')
  37.     if doc_path == 'exit':
  38.         break

  39. file_name = input('请输入需要寻找的目标文件:')

  40. fold = os.walk(doc_path)

  41. flag = 0

  42. all_file = []
  43. for each in fold:
  44.     for each_file_name in each[2]:
  45.         if file_name == each_file_name:
  46.             print(each[0] +'/'+ file_name)
  47.         else:
  48.             flag = 1
  49. if flag == 1:
  50.     print('没有这个文件噢!')

  51. 3.
  52. import os

  53. doc_path = input('请输入待查找的初始目录:')
  54. while not os.path.isdir(doc_path):
  55.     doc_path = input('输入错误,请重新输入:')

  56. doc_walk = os.walk(doc_path)

  57. file_path = []

  58. for each_part in doc_walk:
  59.     for each_file in each_part[2]:
  60.         if '.avi' or '.mp4' or '.rmvb' in each_file.lower():
  61.             file_path.append(os.path.join(each_part[0],each_file) + '\n' * 2)

  62. print(file_path)

  63. f = open('/Users/yufan/Documents/python编程/30sp/videoList.txt','w',encoding = 'GBK')
  64. f.writelines(file_path)
  65. f.close()

  66. 4.
  67. import os

  68. keyword = input('请将该代码放于待查找的文件夹内,请输入关键字:')
  69. allow = input('请问是否需要打印关键字 *%s* 在文件夹中的位置(yes/no):' % keyword)
  70. while allow.lower() != 'yes':
  71.     allow = input('逗你的,输入yes吧:')

  72. cur_doc_path = os.getcwd()

  73. all_file = os.walk(cur_doc_path)

  74. txt_path = []

  75. for each_part in all_file:
  76.     for each_file in each_part[2]:
  77.         if '.txt' in each_file:
  78.             txt_path.append(os.path.join(each_part[0],each_file))

  79. for each_path in txt_path:
  80.     count = 1
  81.     line = []
  82.     position = []
  83.     f = open(each_path,encoding = 'GBK')
  84.     for each_line in f:
  85.         line_position = []
  86.         if keyword in each_line:
  87.             line.append(count)
  88.             for each in range(len(each_line)):
  89.                 if keyword[0] == each_line[each]:
  90.                     line_position.append(each+1)
  91.             position.append(line_position)
  92.         count += 1
  93.     print('在文件*%s*中找到关键字*%s*' % (each_path,keyword))
  94.     for each in range(len(line)):
  95.         print('关键字出现在第 %d 行,第 ' % line[each],position[each],'个位置')
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-10 14:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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