Thu, April 12, 2007
ふたつのファイルの日付を比較して、新しいかどうか調べる Jython編
はじめに指定したファイルが、あとから指定したファイルより新しいかどうか を調べる。
code
import sys
import java.io as io
file1=sys.argv[1]
file2=sys.argv[2]
f1=io.File(file1)
f2=io.File(file2)
if f1.lastModified()>f2.lastModified():
print file1," is newer than ",file2
実行例
$ ls
filechk.py foo.txt foo.html
$ touch foo.txt
$ jython filechk.py foo.txt foo.html
foo.txt is newer than foo.html
$ touch foo.html
$ jython filechk.py foo.txt foo.html
シェルで処理した方が簡単に記述できる(わかりにくいが) jythonの起動は遅いので、簡単な作業ならシェルでやるべし。
ただし、javaプログラムと連携した作業の中で行うならば、 jythonを使うメリットが出てくる可能性がある。