鱼C论坛

 找回密码
 立即注册
查看: 2094|回复: 2

[已解决]如何通过名称找出文件路径

[复制链接]
发表于 2022-7-12 22:46:29 | 显示全部楼层 |阅读模式

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

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

x
如何在idle用python通过文件名称找出它的windowspath

谢谢
最佳答案
2022-7-12 23:16:41
本帖最后由 jackz007 于 2022-7-12 23:28 编辑

        假如要寻找的文件名是 'notepad.exe'
  1. import os

  2. for root , dirs , files in os . walk('C:\\'):
  3.     for file in files:
  4.         if file . lower() == 'notepad.exe':
  5.             print(root)
复制代码

        打印出来的路径中都有要找的文件:'notepad.exe'
        运行实况:
  1. Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>> import os
  4. >>> for root , dirs , files in os . walk('C:\\'):
  5.     for file in files:
  6.         if file . lower() == 'notepad.exe':
  7.             print(root)

  8.             
  9. C:\WINDOWS
  10. C:\WINDOWS\system32
  11. C:\WINDOWS\system32\dllcache
  12. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-7-12 22:59:59 From FishC Mobile | 显示全部楼层
遍历所有目录
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-7-12 23:16:41 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jackz007 于 2022-7-12 23:28 编辑

        假如要寻找的文件名是 'notepad.exe'
  1. import os

  2. for root , dirs , files in os . walk('C:\\'):
  3.     for file in files:
  4.         if file . lower() == 'notepad.exe':
  5.             print(root)
复制代码

        打印出来的路径中都有要找的文件:'notepad.exe'
        运行实况:
  1. Python 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 19:28:18) [MSC v.1600 32 bit (Intel)] on win32
  2. Type "copyright", "credits" or "license()" for more information.
  3. >>> import os
  4. >>> for root , dirs , files in os . walk('C:\\'):
  5.     for file in files:
  6.         if file . lower() == 'notepad.exe':
  7.             print(root)

  8.             
  9. C:\WINDOWS
  10. C:\WINDOWS\system32
  11. C:\WINDOWS\system32\dllcache
  12. >>>
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-28 10:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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