简体   繁体   English

如何使用python检查文件夹是否为快捷方式?

[英]How to check whether a folder is a shortcut or not using python?

I am making a simple program using python which takes two inputs from the user:- filename which is the name of the file which the user wants to search. 我正在使用python制作一个简单的程序,该程序从用户那里接收两个输入:-filename,这是用户要搜索的文件名。 pathname which is the path where the user wants to search the file. pathname是用户要在其中搜索文件的路径。 I am using os module in my code. 我在代码中使用os模块。 But, I want that my program should not search for the file in shortcuts. 但是,我希望我的程序不应该在快捷方式中搜索文件。 So, is there a way by which we can check whether a folder is shortcut or not? 因此,有没有一种方法可以检查文件夹是否为快捷方式? I am posting the definition of my function below : 我在下面发布我的函数的定义:

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)

On Windows, links (shortcuts) have a file type ".lnk" so you could try fn.endswith(".lnk") which will return True for these shortcut files. 在Windows上,链接(快捷方式)的文件类型为".lnk"因此您可以尝试fn.endswith(".lnk") ,这些快捷方式文件将返回True At least on Windows, os.path.islink() just sees a file, unlike on some other OS such as linux which has true links. 至少在Windows上, os.path.islink()只能看到一个文件,这与其他具有真正链接的操作系统(例如linux)不同。

If you want to check for (symbolic) links, please see if os.path.islink suites your needs. 如果要检查(符号)链接,请查看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

I just created a Desktop "shortcut" graphically with nautilus, right clicking folder, chosing "create link" and it just creates a sym link. 我刚刚用鹦鹉螺图形化地创建了一个桌面“快捷方式”,右键单击文件夹,选择了“创建链接”,它只是创建了一个符号链接。 The above script identifies it correctly as a link. 上面的脚本将其正确标识为链接。

It more comment, but for comments not work formatting. 它有更多注释,但对于注释无法使用格式。 I don't understand what mean shorcut but I have hope, below will useful: 我不明白什么是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.

相关问题 使用python检查名称文件夹是否存在 - check whether a folder exists with name using python 如何创建指向 python 中文件夹的 windows 快捷方式 - how to create a windows shortcut to a folder in python 使用Selenium和Python,如何检查按钮是否仍然可点击? - Using Selenium and Python, how to check whether the button is still clickable? 如何使用python检查给定列表中的元素是否为文本? - How to check whether a element from given list is in text or not using python? 如何检查浏览器是否正在使用python运行? - How do I check whether a browser is running using python? 如何使用 Python 检查字符串是否由单个重复数字组成 - How to check whether or not a string consists of a single repeating digit using Python 如何使用Python检查数据是否在CSV文件中 - How to check whether the data is in a CSV file using Python 在使用CGI的python 2.7中,如何检查分叉过程是否完成 - In python 2.7 using CGI, how to check whether forked process completed 如何使用 python 中的递归检查一个字符串是否是另一个字符串的后续? - How to check whether a string is subsequent of another string using recursion in python? 如何检查文件夹是否存在并使用 python 创建文件夹 - how to check if folder exist and create folder using python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM