【发布时间】:2013-09-22 17:27:08
【问题描述】:
我正在使用 python 制作一个简单的程序,它接受用户的两个输入:- filename 是用户想要搜索的文件的名称。 pathname 是用户想要搜索文件的路径。 我在我的代码中使用 os 模块。 但是,我希望我的程序不应该在快捷方式中搜索文件。那么,有没有办法可以检查文件夹是否是快捷方式? 我在下面发布了我的函数的定义:
def searchwithex(path, filen):
global globalFileVal
global globalFileList
global count
dirCounter = checkdir(path) # checks whether the path is accesible or not.
if dirCounter == True:
topList = listdir(path)
for items in topList:
count += 1
if filen == items:
globalFileVal = path +'/' + items
globalFileList.append(globalFileVal)
items = path + '/' + items
if os.path.isdir(items): # checks whether the given element is a #file or a directory.
counter = searchwithex(items, filen)
【问题讨论】:
-
a) 什么是快捷方式?一条链接?符号链接? b) 您正在开发哪个操作系统?
-
@Ashwini:在 Windows 上,
os.path.islink()在给定文件夹的快捷方式(.lnk 文件)路径时返回False。 -
在 Windows 中,快捷方式确实是一个文件,对吧?所以你需要检查内容,不是吗?
-
@Pratik:为了防止循环,您需要跟踪您已经访问过的路径以避免再次跟随它们。您可以通过在
set中跟踪它们并在跟踪遇到的任何子目录之前对其进行检查来做到这一点。