每次更改较少的文件时,我都编写了简单的脚本来更新我的 CSS 样式(从 less 到 css)。我现在有了:
import time
import hashlib
from subprocess import call
def md5_checksum(filePath):
fh = open(filePath, 'rb')
m = hashlib.md5()
m.update(fh.read())
fh.close()
return m.hexdigest()
md5 = md5_checksum('styles.less')
while True:
newmd5 = md5_checksum('styles.less')
if md5 != newmd5:
sh = open('styles.css', 'w')
call(['lessc', 'styles.less'], stdout=sh)
md5 = newmd5
sh.close()
print 'Changed'
time.sleep(0.2)
奇怪的是,脚本工作了一段时间:
Changed
Changed
Changed
Changed
Traceback (most recent call last):
File "watcher.py", line 16, in <module>
newmd5 = md5_checksum('styles.less')
File "watcher.py", line 7, in md5_checksum
fh = open(filePath, 'rb')
IOError: [Errno 2] No such file or directory: 'styles.less'
这是怎么回事?文件仍然存在 100%。我究竟做错了什么?