当前位置:   article > 正文

python,查找电脑上指定格式的文件_python 查看一个文件夹内所有文件的格式

python 查看一个文件夹内所有文件的格式

寻找并列出电脑上除去系统文件夹和管理员文件夹的所有.txt、.exe、.doc、.docx文件,并发送至数据库。

  1. import os
  2. import wmi
  3. import pyodbc
  4. import glob
  5. '''使用 glob的glob.iglob()函数匹配所有文件位置的文件,缺点慢、占用CPU资源较高。'''
  6. # def file_information():
  7. # drives = [chr(x) + ':' for x in range(65, 90) if os.path.exists(chr(x) + ':')]
  8. # text1 = ['/**/*.txt', '/**/*.exe', '/**/*.doc', '/**/*.docx']
  9. # exclude_dirs = ['C:/Users\\Administrator', 'C:/Windows', 'C:/Program Files', 'C:/Program Files (x86)',
  10. # 'C:/ProgramData']
  11. # file_name = []
  12. # file_path = []
  13. # file_size = []
  14. # for drive in drives:
  15. # for a in text1:
  16. # for filename in glob.iglob(drive + a, recursive=True):
  17. # if not any(exclude_dir in filename for exclude_dir in exclude_dirs):
  18. # file_name.append(os.path.basename(filename))
  19. # file_path.append(filename)
  20. # size_a = (os.path.getsize(filename))
  21. # file_size.append(str((int((size_a / 1024) + 1))) + 'KB')
  22. #
  23. # return file_name, file_path, file_size
  24. '''使用os的os.walk()函数递归遍历磁盘,很快,不怎么占用CPU资源,但是运行期间硬盘资源会被占满,机械硬盘的电脑慎用'''
  25. # 返回值:str文件名、str文件路径、str文件大小
  26. def file_information():
  27. file_name = []
  28. file_path = []
  29. file_size = []
  30. # 获取计算机所有的盘符
  31. drives = [chr(x) + ':' for x in range(65, 90) if os.path.exists(chr(x) + ':')]
  32. for drive in drives:
  33. for root, dirs, files in os.walk((drive + "/")):
  34. # 排除系统文件夹和管理员文件夹
  35. dirs[:] = [d for d in dirs if
  36. d not in ['Windows', 'Program Files', 'Administrator', 'Program Files (x86)', 'ProgramData']]
  37. for file in files:
  38. # 筛选带txt、exe、doc、docx类型的文件
  39. if file.endswith(".txt") or file.endswith(".exe") or file.endswith(".doc") or file.endswith(".docx"):
  40. file_name.append(file) # 文件名
  41. file_path.append(os.path.join(root, file)) # 文件路径
  42. file_size.append(
  43. str((int(((os.path.getsize(os.path.join(root, file))) / 1024) + 1))) + 'KB') # 文件大小
  44. return file_name, file_path, file_size
  45. # 返回值:str电脑名、str电脑使用人
  46. def computer_information():
  47. # 读取电脑配置信息
  48. w = wmi.WMI()
  49. pc_name = '' # 电脑名称
  50. user_name = '' # 电脑使用人
  51. for BIOSs in w.Win32_ComputerSystem():
  52. pc_name += BIOSs.Caption # 电脑名称
  53. user_name += BIOSs.UserName # 电脑使用人
  54. user_name = user_name.replace('\\', '-') # 替换符号
  55. return pc_name, user_name
  56. def sql_statement(lile_list, pc_information):
  57. # 连接到SQL Server数据库
  58. conn = pyodbc.connect('DRIVER={SQL Server};SERVER=数据库地址;DATABASE=数据库名称;UID=账号;PWD=密码')
  59. # 创建游标
  60. cursor = conn.cursor()
  61. # SQL命令,删除电脑名为指定名称的所有条目。
  62. data2 = "if exists(select * from 表名 where 字段—电脑名='" + pc_information[
  63. 0] + "') begin delete from 表名 where 字段—电脑名='" + pc_information[0] + "' end"
  64. insert_query = "INSERT INTO 表名 (字段—文件名, 字段—文件路径, 字段—电脑名, 字段—电脑使用人, 字段—文件大小) VALUES (?, ?, ?, ?, ?)"
  65. data1 = []
  66. for i in range(len(lile_list[0])):
  67. data1.append((lile_list[0][i], lile_list[1][i], pc_information[0], pc_information[1], lile_list[2][i]))
  68. # 执行判断删除语句
  69. cursor.execute(data2)
  70. # 执行插入操作
  71. cursor.executemany(insert_query, data1)
  72. # 提交事务
  73. conn.commit()
  74. # 关闭连接
  75. conn.close()
  76. file_i = file_information()
  77. computer_i = computer_information()
  78. sql_statement(file_i, computer_i)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/319382
推荐阅读
相关标签
  

闽ICP备14008679号