鱼C论坛

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

[学习笔记] 【Python学习笔记】第030讲,文件系统 os

[复制链接]
发表于 2018-6-29 18:29:41 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 qq33zz 于 2018-6-29 18:31 编辑

动动手0
  1. import os                                                     
  2. all_files = os.listdir(os.curdir)                                       
  3. type_dict = {}

  4. for each_file in all_files:
  5.     if os.path.isdir(each_file):
  6.         type_dict.setdefault('文件夹', 0)
  7.         type_dict['文件夹'] += 1
  8.     else:
  9.         ext = os.path.splitext(each_file)[1]
  10.         type_dict.setdefault(ext, 0)
  11.         type_dict[ext] += 1

  12. for each_type in type_dict.keys():
  13.     print('该文件夹下共有类型为【%s】的文件 %d 个' % (each_type, type_dict[each_type]))
复制代码


动动手1
  1. import os                                                       #计算文件大小
  2. all_files = os.listdir(os.curdir)

  3. for each_file in all_files:
  4.     if os.path.isfile(each_file):
  5.         print('%s[%d]'%(each_file,os.path.getsize(each_file)))
  6.     elif os.path.isdir(each_file):
  7.         print('文件夹名:%s'%each_file)

  8. import os
  9. def defind(star_path,file_input):                                 
  10.     os.chdir(star_path)
  11.     for each_file in os.listdir(os.curdir):                       
  12.         if each_file == file_input:                              
  13.             print(os.getcwd()+os.sep+each_file)
  14.         if os.path.isdir(each_file):
  15.             defind(each_file,file_input)
  16.             os.chdir(os.pardir)

  17. star_path =  input('请输入需要搜索的路径:')
  18. file_input = input('请输入需要查找的文件名:' )
  19. defind(star_path,file_input)
复制代码


动动手2
  1. import os                                                  #查找路径下指定后缀名
  2. def defind(star_path,suffix):
  3.     os.chdir(star_path)
  4.     for each_file in os.listdir(os.curdir):
  5.         if os.path.splitext(each_file)[1] in suffix:
  6.             file.write(os.getcwd()+os.sep+each_file+'\n')
  7.         if os.path.isdir(each_file):
  8.             defind(each_file,suffix)
  9.             os.chdir(os.pardir)
  10. file = open('list.txt', 'a')
  11. suffix = ['.txt','.py']
  12. star_path = input('请输入需要查询的路径:')
  13. defind(star_path,suffix)
复制代码


动动手3
  1. import os
  2. def scan_file(key,user_input):                               #查找文件
  3.     txt_file=[]
  4.     all_file = os.walk(os.getcwd())
  5.     for i in all_file:
  6.         for each_file in i[2]:
  7.             if os.path.splitext(each_file)[1] == '.txt':
  8.                 each_file=os.path.join(i[0],each_file)
  9.                 txt_file.append(each_file)
  10.     for file in txt_file:
  11.         str_position = scan_line(file, key)
  12.         if str_position:                                               
  13.             print('=================================================================')
  14.             print('在【%s】文件中发现【%s】关键词' % (file, key))
  15.         for k in str_position.keys():
  16.             if user_input in ['Y','y']:
  17.                 print('关键字被发现在第【%s】行第【%s】个字符'%(k,str_position[k]))
  18.     return txt_file

  19. def scan_line(file1,key):                       #查找行
  20.     line_result = {}
  21.     count_line = 0
  22.     b = open(file1)
  23.     for each_line in b.readlines():
  24.         count_line += 1
  25.         if key in each_line:
  26.             line_result[count_line] = scan_str(each_line,key)
  27.     b.close()
  28.     return line_result

  29. def scan_str(line,key):                        #查找字符
  30.     str_result = []
  31.     while line.find(key) != -1:
  32.         str_result.append(line.find(key))
  33.         break
  34.     return str_result

  35. key = 'MJ'
  36. user_input = 'Y'
  37. scan_file(key,user_input)
复制代码

知识点:
1、python 把 0、空字符串、和none看成False。
2、return可以返回多个值。
3、os.walk()查找路径下所有文件包括子文件夹,返回一个三元组(dirpath目录路径,dirname目录下所有文件夹名,filename目录下所有文件名)。
4、os.curdir当前目录(没有参数)、¥os.chdir()路径跳转、os.pardir上级路径、¥os.getcwd()获取当前路径名、os.sep输入\、¥os.listdir()获取路径下所有文件
5、os.find()用于查找字符串在文件中的位置。差找不到返回-1
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-21 14:04

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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