[英]How to check whether a folder is a shortcut or not using python?
我正在使用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)
在Windows上,鏈接(快捷方式)的文件類型為".lnk"
因此您可以嘗試fn.endswith(".lnk")
,這些快捷方式文件將返回True
。 至少在Windows上, os.path.islink()
只能看到一個文件,這與其他具有真正鏈接的操作系統(例如linux)不同。
如果要檢查(符號)鏈接,請查看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
我剛剛用鸚鵡螺圖形化地創建了一個桌面“快捷方式”,右鍵單擊文件夾,選擇了“創建鏈接”,它只是創建了一個符號鏈接。 上面的腳本將其正確標識為鏈接。
它有更多注釋,但對於注釋無法使用格式。 我不明白什么是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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.