以下の内容はhttps://takuya-1st.hatenablog.jp/entry/2016/04/25/034125より取得しました。


Python で空じゃないディレクトリを削除する方法

Pythonでディレクトリを削除しようとすると os.rmdir だけど

import os 

os.rmdir("/path/to/not_empty_dir")

実行すると not empty で止まる。

OSError: [Errno 39] Directory not empty:

ま、当然なんだけど。

消す方法

  • shell をcall して rm -rf path/to/dir する
  • shutil.rmtree を使う

シェル経由で rmdir 呼び出すのはまぁ、想像つくんだけど、ruby の FileUtil に相当するものがないのか調べたら、shutilがそれらしい。

shutil.rmtreeの場合

コレが確実っぽい

import shutil
shutil.rmtree("path/to/dir")

これで、確実に作業用ディレクトリを捨てられる。

参考資料

http://stackoverflow.com/questions/13766513/how-to-do-force-remove-in-python-like-rm-rf-on-linux




以上の内容はhttps://takuya-1st.hatenablog.jp/entry/2016/04/25/034125より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14