1

私は、ユーザーから2つの入力を受け取るpythonを使用して簡単なプログラムを作成しています:- ユーザーが検索したいファイルの名前であるファイル名。ユーザーがファイルを検索したいパスであるパス名。コードで 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)
4

3 に答える 3

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でグラフィカルに作成し、フォルダーを右クリックして「リンクの作成」を選択すると、シンボリックリンクが作成されます。上記のスクリプトは、それをリンクとして正しく識別します。

于 2013-09-18T05:33:33.820 に答える