このコードは機能します。〜100個のファイルとフォルダーがすばやく作成および破棄される単体テストで使用しています。
sleep() ステートメントを取り出すと、Directory does not exist エラーや Directory Permissions エラーなど、さまざまな Windows エラーが発生します。
かなり醜い「ハック」のように見えるスリープメッセージが含まれていることに当惑しています。明らかに、私のターゲット システムでは、sleep(1) が正しい値であることを知りません。任意の 1 秒の遅延に依存しない適切な成人向けソリューションは何ですか (最終的なシステムは時々重負荷になります)?
def removeDirectoryBranch(myPath):
# delete a directory path and the files/folders in that directory
sleep(1) # give the OS time to sort itself out
for root, dirs, files in walk(myPath, topdown=False):
# safety check removed for for this question
# limits the scope of the function
for name in files:
remove(join(root, name))
for name in dirs:
rmdir(join(root, name))
rmdir(myPath)
Windows 7 上で動作する Python 3.3 (Eclipse および PyDev を使用)
上記のコードは、単体テスト クラスの 'teardown' メソッドにあります。すべてのファイルが適切に開かれ、閉じられます (try/except/finish 構造で)
unitttest は毎回同じように実行されますが、問題はランダムに発生します (ほとんどの場合、スリープは必要ありません)。