我有一个 python 脚本,可以搜索目录中的文件,并在计算机运行时无限执行。这是代码:
import fnmatch
import os
import shutil
import datetime
import time
import gc
# This is a python script that removes the "conflicted" copies of
# files that dropbox creates when one computer has the same copy of
# a file as another computer.
# Written by Alexander Alvonellos
# 05/10/2012
class cleanUpConflicts:
rootPath = 'D:\Dropbox'
destDir = 'D:\Conflicted'
def __init__(self):
self.removeConflicted()
return
def cerr(message):
f = open('./LOG.txt', 'a')
date = str(datetime.datetime.now())
s = ''
s += date[0:19] #strip floating point
s += ' : '
s += str(message)
s += '\n'
f.write(s)
f.close()
del f
del s
del date
return
def removeConflicted(self):
matches = []
for root, dirnames, filenames in os.walk(self.rootPath):
for filename in fnmatch.filter(filenames, '*conflicted*.*'):
matches.append(os.path.join(root, filename))
cerr(os.path.join(root, filename))
shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))
del matches
return
def main():
while True:
conf = cleanUpConflicts()
gc.collect()
del conf
reload(os)
reload(fnmatch)
reload(shutil)
time.sleep(10)
return
main()
反正。有一个内存泄漏,每十秒左右增加近 1 兆。我不明白为什么没有释放内存。到最后,这个脚本会不断地吃掉内存,甚至不用尝试。这令人沮丧。有人有任何提示吗?我什么都试过了,我想。
这是进行此处建议的一些更改后的更新版本:
import fnmatch
import os
import shutil
import datetime
import time
import gc
import re
# This is a python script that removes the "conflicted" copies of
# files that dropbox creates when one computer has the same copy of
# a file as another computer.
# Written by Alexander Alvonellos
# 05/10/2012
rootPath = 'D:\Dropbox'
destDir = 'D:\Conflicted'
def cerr(message):
f = open('./LOG.txt', 'a')
date = str(datetime.datetime.now())
s = ''
s += date[0:19] #strip floating point
s += ' : '
s += str(message)
s += '\n'
f.write(s)
f.close()
return
def removeConflicted():
for root, dirnames, filenames in os.walk(rootPath):
for filename in fnmatch.filter(filenames, '*conflicted*.*'):
cerr(os.path.join(root, filename))
shutil.move(os.path.join(root, filename), os.path.join(destDir, filename))
return
def main():
#while True:
for i in xrange(0,2):
#time.sleep(1)
removeConflicted()
re.purge()
gc.collect()
return
main()
我已经对这个问题进行了一些研究,似乎 fnmatch 中可能存在一个错误,它有一个使用后不会清除的正则表达式引擎。这就是我调用 re.purge() 的原因。我已经修改了几个小时了。
我还发现这样做:
print gc.collect()
每次迭代返回 0。
谁对我投了反对票,显然是错误的。我真的需要一些帮助。这是我正在谈论的链接:为什么我用这个 python 循环泄漏内存?