2016年2月20日土曜日

pythonのスクリプトファイルとの相対パス

import os

this_dir = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(this_dir, 'lib')
print filename

2016年2月19日金曜日

python によるPID のロック

import time
import os
import sys

pid = str(os.getpid())
pidfile = "/tmp/scripy.pid"

if os.path.isfile(pidfile):
    print "exit"
    sys.exit(0)
file(pidfile, 'w').write(pid)
try:
    f = open('workfile', 'a')
    f.write("1")
    f.close
    time.sleep(10)

finally:
    os.unlink(pidfile)