Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2025/09/05 15:49:41.256 (台灣標準時)

Python の shutil モジュールを使ってファイルをコピーする

shutil モジュールを使ったファイルのコピー

Python の shutil モジュールを使ってファイルをコピーする方法は以下の通 り。 shutil.copy2 を使う。


#!/usr/pkg/bin/python3.9

# importing shutil module
import shutil

# importing pathlib module
import pathlib

# source file
file_src = '/netbsd'

# destination file
file_dst = '/tmp/netbsd_kernel'

# constructing pathlib object
p_src = pathlib.Path (file_src)
p_dst = pathlib.Path (file_dst)

# existence check
e_src = p_src.exists ()
e_dst = p_dst.exists ()

# printing result of existence checks
print ("existence of source file      =", e_src)
print ("existence of destination file =", e_dst)

# copying file
print ("Now copying file...")
shutil.copy2 (p_src, p_dst)
print ("Finished!")

# existence check
e_src = p_src.exists ()
e_dst = p_dst.exists ()

# printing result of existence checks
print ("existence of source file      =", e_src)
print ("existence of destination file =", e_dst)

# stat objects
s_src = p_src.stat ()
s_dst = p_dst.stat ()

# printing stat objects
print ("%s:" % file_src)
print (s_src)
print ("%s:" % file_dst)
print (s_dst)

実行した結果は以下の通り。
% ./test_shutil_0.py
existence of source file      = True
existence of destination file = False
Now copying file...
Finished!
existence of source file      = True
existence of destination file = True
/netbsd:
os.stat_result(st_mode=33261, st_ino=3, st_dev=4864, st_nlink=1, st_uid=0, st_gid=0, st_size=24830248, st_atime=1612588967, st_mtime=1611376463, st_ctime=1611376463)
/tmp/netbsd_kernel:
os.stat_result(st_mode=33261, st_ino=3757717, st_dev=4864, st_nlink=1, st_uid=XXX, st_gid=XXX, st_size=24830248, st_atime=1612588967, st_mtime=1611376463, st_ctime=1612588967)

参考文献



Frequently accessed files

  1. Misc___Taiwan/20240207_00.html
  2. Misc___Taiwan/20240819_00.html
  3. Computer___NetBSD/20230119_00.html
  4. Computer___TeX/20231107_00.html
  5. Computer___NetBSD/20250301_01.html
  6. Computer___TeX/20230726_01.html
  7. Misc___Taiwan/20240903_01.html
  8. Computer___Network/20240130_00.html
  9. Misc___Japan/20240610_00.html
  10. Computer___NetBSD/20240805_03.html
  11. Computer___TeX/20240411_00.html
  12. Computer___WWW/20230522_02.html
  13. Food___Taiwan/20230608_04.html
  14. Misc___Taiwan/20250728_01.html
  15. Computer___FreeBSD/20220621_0.html
  16. Computer___NetBSD/20250409_00.html
  17. Science___Math/20220420_0.html
  18. Computer___Hardware/20240820_00.html
  19. Computer___TeX/20230503_00.html
  20. Computer___NetBSD/20241102_00.html
  21. Misc___Taiwan/20220831_4.html
  22. Computer___NetBSD/20250307_00.html
  23. Computer___TeX/20240410_00.html
  24. Computer___Android/20250616_00.html
  25. Computer___TeX/20240414_01.html
  26. Computer___Debian/20230102_00.html
  27. Computer___NetBSD/20240810_00.html
  28. Computer___iPad/20250507_00.html
  29. Computer___NetBSD/20220818_1.html
  30. Computer___TeX/20240414_00.html


HTML file generated by Kinoshita Daisuke.