鱼C论坛

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

[技术交流] python 030 os模块

[复制链接]
发表于 2018-6-26 12:56:10 | 显示全部楼层 |阅读模式

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

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

x
1. 调用os模块: import os, os模块中的函数使用方法:http://bbs.fishc.com/thread-45512-1-1.html

2.  030 动动手的程序实现:

  1. import  os

  2. def  seek_extension(path):
  3.     file_type = {}
  4.     file = os.listdir(path)
  5.     for  each_file in file:
  6.         file_name_extension = os.path.splitext(each_file)
  7.         if  file_name_extension[1] not in file_type:
  8.             file_type[file_name_extension[1]] = 1
  9.         else:
  10.             file_type[file_name_extension[1]] += 1

  11.     if  '' in file_type:
  12.         file_type.setdefault('文件夹', file_type[''])
  13.         file_type.pop('')

  14.     for  eachkey in file_type.keys():
  15.         print("该文件夹下共有类型为【%s】的文件 %d 个" % ( eachkey, file_type[eachkey] ) )


  16. path = input("输入要查找的绝对路径: ")
  17. seek_extension(path)
复制代码

  1. import os

  2. def  file_size_calc(path):
  3.     file = os.listdir(path)
  4.     file_size = {}
  5.     for  each_file in file:
  6.         file_size[each_file] = os.path.getsize(path + '\\' + each_file)         #os.path.getsize(path) 中 path为绝对路径

  7.     for  each_key in file_size.keys():
  8.         print('%s 【%d bytes】' % ( each_key, file_size[each_key] ) )


  9. path = input("请输入要查找位置的绝对路径: ")
  10. file_size_calc(path)
复制代码

  1. import os

  2. def  find_file(path, file):
  3.     os.chdir(path)
  4.    
  5.     for  each_file in os.listdir( os.curdir ):
  6.         if  os.path.isdir(each_file):
  7.             find_file(each_file, file)
  8.             os.chdir('..')
  9.         if  each_file == file:
  10.             print(os.getcwd() + os.sep + each_file)

  11. path = input("请输入待查找的初始目录: ")
  12. file = input("请输入需要查找的目标文件: ")
  13. find_file(path, file)
复制代码

  1. import os
  2. file_format = ['.mp4', '.rmvb', '.avi']
  3. save_filepath = list()

  4. def  search_file(path):
  5.     os.chdir(path)

  6.     for  each_file in os.listdir(os.curdir):
  7.         if  os.path.isdir(each_file):
  8.             search_file(each_file)
  9.             os.chdir(os.pardir)
  10.         if  os.path.splitext(each_file)[1] in file_format:
  11.             save_filepath.append(os.getcwd() + os.sep + each_file)

  12.     f = open('D:\\python\\python_study\\vediolist.txt', 'w')
  13.     for  each in save_filepath:
  14.         f.writelines(each + '\n')

  15.     f.close()

  16. path = input("请输入带查找的初始目录: ")
  17. search_file(path)
复制代码

  1. import os

  2. def  pos_in_line(line, key):
  3.     pos = []
  4.     begin = line.find(key)

  5.     while  begin != -1:
  6.         pos.append(begin + 1)
  7.         begin = line.find(key, begin + 1)

  8.     return pos

  9. def  pos_in_file(file, key):
  10.     dict_key = dict()
  11.     count = 0
  12.     target_file = open(file)

  13.     for  each_line in target_file:
  14.         count += 1
  15.         if  key in each_line:
  16.             pos = pos_in_line(each_line, key)
  17.             dict_key[count] = pos

  18.     target_file.close()
  19.     return dict_key

  20. def  print_key(dict_key):
  21.     keys = dict_key.keys()
  22.     keys = sorted(keys)
  23.     for  each_key in keys:
  24.         print("关键字出现在第 %s 行, 第 %s 个位置" % ( each_key, dict_key[each_key] ) )
  25.         

  26. def  search_key(path, key):
  27.     os.chdir(path)

  28.     for  each_file in os.listdir(os.curdir):
  29.         if  os.path.isdir(each_file):
  30.             search_key(each_file, key)
  31.             os.chdir(os.pardir)
  32.         if  os.path.isfile(each_file):
  33.             if  os.path.splitext(each_file)[1] == '.txt':
  34.                 dict_key = pos_in_file(each_file, key)
  35.                 if  dict_key != {}:
  36.                     print('==================================================================================')
  37.                     print("在文件【%s】中找到关键字【%s】" %( (os.getcwd() + os.sep + each_file), key ) )
  38.                     print_key(dict_key)


  39. path = input("请输入要查找的路径: ")
  40. key = input("请输入要查找的关键字: ")
  41. search_key(path, key)
复制代码

本帖被以下淘专辑推荐:

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-26 22:33

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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