Reputation: 6492
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. pathname which is the path where the user wants to search the file. I am using os module in my code. 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)
Upvotes: 1
Views: 3238
Reputation: 31
On Windows, links (shortcuts) have a file type ".lnk"
so you could try fn.endswith(".lnk")
which will return True
for these shortcut files. At least on Windows, os.path.islink()
just sees a file, unlike on some other OS such as linux which has true links.
Upvotes: 3
Reputation: 32439
If you want to check for (symbolic) links, please see if os.path.islink
suites your needs.
$ 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.
Upvotes: 0
Reputation: 4462
It more comment, but for comments not work formatting. I don't understand what mean shorcut
but I have hope, below will useful:
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
Upvotes: 0