1

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)
martineau
  • 119,623
  • 25
  • 170
  • 301
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
  • Related: [Check if file is symlink in python](http://stackoverflow.com/questions/11068419/check-if-file-is-symlink-in-python) – Ashwini Chaudhary Sep 18 '13 at 04:44
  • 6
    a) What is a shortcut? A link? A symbolic link? and b) Which OS are you working on? – Hyperboreus Sep 18 '13 at 04:45
  • @Ashwini: On Windows `os.path.islink()` returns `False` when given a path to a shortcut (a .lnk file) to a folder. – martineau Sep 18 '13 at 04:52
  • in Windows, a shortcut is indeed a file, right? So you need to check the content, no? – justhalf Sep 18 '13 at 04:55
  • @AshwiniChaudhary symlink is not the same as shortcut, i tried the solution given in [link](http://stackoverflow.com/questions/11068419/check-if-file-is-symlink-in-python) – Pratik Singhal Sep 18 '13 at 04:57
  • @Hyperboreus I am working on ubuntu 12.04. – Pratik Singhal Sep 18 '13 at 04:59
  • Suppose, I have a folder A containing shortcut to A and some other files then my above function definitions keeps on running endlessly, because it again and again opens folder A on seeing the shortcut to A.I need to eliminate this problem. – Pratik Singhal Sep 18 '13 at 05:01
  • 1
    @Pratik: To prevent loops you'll need to keep track of what paths you've already visited to avoid following them again. You could do this by keeping track of them all in a `set` and checking it before following any subdirectories encountered. – martineau Sep 18 '13 at 05:08
  • @justhalf: On Windows it's a file, but I'm not sure of the format (which is not plain text). But that doesn't matter because the OP's using ubuntu, so must be talking about a link. – martineau Sep 18 '13 at 05:14
  • @martineau I tried your solution. but its not working because OS treats the shortcut itself as a direct0ry. What I mean is that it shows the path as /A/A/A and so on which depends on how many time you opened the directory. – Pratik Singhal Sep 18 '13 at 05:20
  • I still don't undestand what a shortcut is, especially under ubuntu. – Hyperboreus Sep 18 '13 at 05:24
  • [Possibly relevant.](http://www.howtogeek.com/106470/create-desktop-shortcuts-in-ubuntu-11.04-and-11.10/) AskUbuntu might have people with more knowledge of the subject. – user2357112 Sep 18 '13 at 05:26
  • @Pratik: Try storing the [`os.path.abspath()`](http://docs.python.org/2/library/os.path.html?highlight=abspath#os.path.abspath) of each subdirectory in the `set` (and be sure to converting to that before checking to see if you've already seen one). – martineau Sep 18 '13 at 05:27
  • @Hyperboreus: I think they're symbolic links because I read where you can create them on ubuntu manually using the `ln -s ...` command. – martineau Sep 18 '13 at 05:30
  • Here is what I mean by a shrortcut – Pratik Singhal Sep 18 '13 at 05:46

3 Answers3

3

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.

Heikki
  • 2,214
  • 19
  • 34
Charles F
  • 31
  • 2
0

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.

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
0

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
Michael Kazarian
  • 4,376
  • 1
  • 21
  • 25