かべぎわブログ

ブログです

Pythonのshutilのcopyfileでファイルをコピーする

概要

Pythonのshutilcopyfileを利用してファイルをコピーしてみようと思います。

コード例

shutil.copyfile('コピーもと', 'コピーさき')
です。

実行してみる

saki.txtなんてものがないことを確認(moto.txtはある)

$ ls -l moto.txt saki.txt
ls: saki.txt にアクセスできません: No such file or directory
-rw-rw-r-- 1 ec2-user ec2-user 7  5月 30 10:48 moto.txt

copyfileを実行

$ ./shutil_copyfile.py 

ファイルがコピーできました!!!

$ ls -l moto.txt saki.txt
-rw-rw-r-- 1 ec2-user ec2-user 7  5月 30 10:48 moto.txt
-rw-rw-r-- 1 ec2-user ec2-user 7  5月 30 10:51 saki.txt

おわりに

コピーは大事。

入門 Python 3

入門 Python 3