1

基本的に、Python 2.6 で記述された FileExplorer クラスがあります。それはうまく機能し、ドライブ、フォルダーなどをナビゲートできます。ただし、特定のフォルダー'C:\Documents and Settings/ .*'* に到達すると、スクリプトの基になっている os.listdir がこのエラーをスローします:

WindowsError: [エラー 5] アクセスが拒否されました: 'C:\Documents and Settings/ . '

何故ですか?このフォルダが読み取り専用だからでしょうか? それとも、Windows が保護していて、私のスクリプトがアクセスできないものですか?!

問題のあるコードは次のとおりです(3行目):

def listChildDirs(self):
    list = []
    for item in os.listdir(self.path):
        if item!=None and\
            os.path.isdir(os.path.join(self.path, item)):
            print item
            list.append(item)
        #endif
    #endfor
    return list
4

2 に答える 2

3

Vista 以降では、C:\Documents and Settings はジャンクションであり、実際のディレクトリではありません。

あなたはそれでストレートをすることさえできませんdir

C:\Windows\System32>dir "c:\Documents and Settings"
 Volume in drive C is OS
 Volume Serial Number is 762E-5F95

 Directory of c:\Documents and Settings

File Not Found

悲しいことに、を使用os.path.isdir()すると、返されますTrue

>>> import os
>>> os.path.isdir(r'C:\Documents and Settings')
True

Windowsでシンボリックリンクを処理するためのこれらの回答を見ることができます。

于 2013-02-05T15:37:47.033 に答える
0

おそらく、ディレクトリ アクセスのパーミッション設定であるか、ディレクトリが存在しないことさえあります。スクリプトを管理者として実行する (つまり、すべてにアクセスできる) か、次のようなことを試すことができます。

def listChildDirs(self):
    list = []
    if not os.path.isdir(self.path):
        print "%s is not a real directory!" % self.path
        return list
    try:
        for item in os.listdir(self.path):
            if item!=None and\
                os.path.isdir(os.path.join(self.path, item)):
                print item
                list.append(item)
            #endif
        #endfor
    except WindowsError:
        print "Oops - we're not allowed to list %s" % self.path
    return list

ところで、聞いたことありos.walkますか?あなたが達成しようとしているものの近道かもしれません。

于 2013-02-05T15:26:26.423 に答える