2016-12-30 8 views
5

AttributeErrorは、Pythonのドキュメント(here)のサンプルコードを使用すると発生します。次のようにコード例は次のとおりとos.scandir()はAttributeErrorを発生させます。__exit__

with os.scandir(path) as it: 
    for entry in it: 
     if not entry.name.startswith('.') and entry.is_file(): 
      print(entry.name) 

結果はAttributeErrorある:変数にos.scandir()を割り当てることは正常に動作

D:\Programming>test.py 
Traceback (most recent call last): 
    File "D:\Programming\test.py", line 3, in <module> 
    with os.scandir() as it: 
AttributeError: __exit__ 

、けれども。 誰かが私に行方不明を教えてもらえますか?

答えて

4

コンテキストマネージャのサポートは、それがコンテキストマネージャではありません(とPythonが最初__exit__をロードしようとする)ので、ご覧のエラーが発生します以前のバージョンでそれを使用しようと、Pythonの3.6に追加されました。

This is stated in its documentationscandirのために(は右下のコードは、あなたが見たスニペット):(重点鉱山)

のPython 3.6への更新や、どちらか

New in version 3.6: Added support for the context manager protocol and the close() method. [...]

あなたができる場合は、あなたがすることはできませんコンテキストマネージャとして使用しないでください。

2

ドキュメントは、おそらく古いPythonのバージョンを実行している

New in version 3.6: Added support for the context manager protocol

を言います。

+0

それです。私はまだ3.5.2.3.6で完璧に動作していました、ありがとう! –

関連する問題