LoginSignup
2
0

More than 5 years have passed since last update.

python > shutil.copytree() > ファイルを上書きしない > 代わりに distutils.dir_util.copy_tree() を使う

Last updated at Posted at 2015-12-12
動作確認
Raspberry Pi2 + raspbian

ラインモニタのログ・ファイルをフォルダ単位でコピーするため、以下のようにしていた。

shutil.copytree(srcpath, dstpath + "/Log/")

しかしながら、昨夜、連続通信試験をしながらログをとっても、昼の作業のログしか取れてなかった。ファイル上書きに失敗しているようだった。 

参考1. 上書きできないという質問 @ SO
参考2. distutils.dir_util.copy_tree()を使うには @ SO

以下のように変更した。
変更コミット @ github

 #!/usr/bin/env python

 '''
+v0.2  2015 Dec. 13
+  - fix bug > did not overwrite files 
 v0.1  2015 Dec. 12
   - add USB memory insertion recognition
   - add folder copy feature
 @@ -20,6 +22,8 @@
 import os.path
 import sys
 import time
+import distutils
+from distutils import dir_util

 param = sys.argv

 @@ -36,7 +40,7 @@
    if os.path.isdir(srcpath) and os.path.isdir(dstpath):
        if chk1==False and chk2==False and chk3==True:
            print "inserted"
-           shutil.copytree(srcpath, dstpath + "/Log/")
+           distutils.dir_util.copy_tree(srcpath, dstpath + "/Log/")
            print "Copied"
    time.sleep(0.5)

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0