我正在使用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)
答案 0 :(得分:0)
如果您想检查(符号)链接,请查看os.path.islink
是否符合您的需求。
$ touch a
$ ln -s a b
$ python
Python 2.7.4 (default, Apr 19 2013, 18:28:01)
[GCC 4.7.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os.path as _
>>> _.islink ('a')
False
>>> _.islink ('b')
True
我刚用nautilus以图形方式创建了一个桌面“快捷方式”,右键单击文件夹,选择“创建链接”,它只是创建了一个sym链接。上面的脚本将其正确识别为链接。
答案 1 :(得分:0)
它更多评论,但对于评论不起作用格式。我不明白是什么意思shorcut
但我有希望,下面有用:
In [1]: import os
In [2]: files = os.listdir("./tmp/11");
In [3]: print files
['mylog', 'testfile1', 'test.py', 'testfile0', 'test.sh', 'myflags', 'testfile2']
In [4]: True if "test.py" in files else False
True