|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
可以指定一个目录,但是不能直接从D盘找。
请输入要查找的初始目录:D:\\
请输入要查找的目标文件:SSOCommon.dll
Traceback (most recent call last):
File "C:/Users/10194/Desktop/Python练习/查找指定目录的指定文件.py", line 15, in <module>
search_file(start_dir,target)
File "C:/Users/10194/Desktop/Python练习/查找指定目录的指定文件.py", line 10, in search_file
search_file(each_file,target)
File "C:/Users/10194/Desktop/Python练习/查找指定目录的指定文件.py", line 10, in search_file
search_file(each_file,target)
File "C:/Users/10194/Desktop/Python练习/查找指定目录的指定文件.py", line 4, in search_file
os.chdir(start_dir)
PermissionError: [WinError 5] 拒绝访问。: 'S-1-5-21-742350292-342748455-3618356719-500'
>>>
========== RESTART: C:/Users/10194/Desktop/Python练习/查找指定目录的指定文件.py ==========
请输入要查找的初始目录:D:\\Bin
请输入要查找的目标文件:SSOCommon.dll
D:\Bin\TXSSO\bin\SSOCommon.dll
但是在IDLE中进行操作可以直接访问显示D盘
>>> os.getcwd()
'D:\\'
>>> os.listdir(os.curdir)
['$RECYCLE.BIN', '.temp', 'AppStore', 'AuI18N', 'BaiduNetdiskDownload', 'Bin', 'c++', 'CENTOS', 'centos镜像文件', 'CloudMusic', 'codeblocks', 'common.xml.txd', 'config.xml.txd', 'default', 'DevC++', 'gf-config-postlogin.xml', 'gf-config.xml', 'Haozip', 'Hbuider', 'I18N', 'Java', 'Misc', 'oj练习题', 'PhoneGame', 'platform.tpc', 'Plugin', 'Program Files', 'PS', 'Python', 'Python虚拟机副本', 'QMDownload', 'QQLicense.rtf', 'qqpcmgr_docpro', 'QQUninst.exe']
源代码
import os
def search_file(start_dir,target):
os.chdir(start_dir)
for each_file in os.listdir(os.curdir ):
if each_file == target:
print(os.getcwd() + os.sep + each_file)
if os.path.isdir(each_file):
search_file(each_file,target)
os.chdir(os.pardir)
start_dir = input('请输入要查找的初始目录:')
target = input ('请输入要查找的目标文件:')
search_file(start_dir,target)
目录访问权限问题已经有过讨论,建议楼主参考一下此贴:
https://fishc.com.cn/forum.php?mod=redirect&goto=findpost&ptid=130476&pid=3748614
|
|