リストに基づきファイルコピィ
import os import sys import shutil import string separator = "," def copy_file(src, dst) : if os.path.exists(src) : if os.path.isdir(src) : if os.path.exists(dst) : os.removedirs(dst) shutil.copytree(src, dst) elif os.path.isfile(src) : if not os.path.exists(dst) : print dst + " is not existed." os.makedirs(dst) print dst + " has been made." shutil.copy2(src, dst) else : print src + " is not existed." sys.exit() def read_list(files_list) : fl = open(files_list, "r") lines = fl.readlines() for line in lines : f_pairs = string.split(line, separator) src_file = f_pairs[0] dst_dir = string.rstrip(f_pairs[1]) copy_file(src_file, dst_dir) fl.close() if __name__=='__main__' : if len(sys.argv)==2 : read_list(sys.argv[1]) else : print ">python copyfiles.py <files_list>"
例:files_list
C:\temp\html\index.html,D:\www\~laclef\ C:\temp\html\index1.cgi,D:\www\~laclef\ C:\temp\html\logo.jpg,D:\www\~laclef C:\temp\html\new.html,D:\www\~laclef\ ../mp3/test,mp3\trash
> python copyfiles.py files_list
引数に与えられた一定の書式で書かれているファイルを参照して、ファイルをコピィします。
コピィリストファイルの書式は、
<コピィ元ファイル or ディレクトリ><separator><コピィ先ディレクトリ>
コピィ元ファイル or ディレクトリが無い場合は終了します。
コピィ先ディレクトリが無い場合は作成します。
注意:ディレクトリコピィの場合、コピィ先ディレクトリに指定したディレクトリと同名のディレクトリが先に存在していた時は、先に存在したディレクトリを削除します。
ファイル・ディレクトリ指定は絶対・相対両方OKです。
Python書いたの、久しぶり。