文件句柄没有被关闭释放 [英] File handle not being released by close

查看:648
本文介绍了文件句柄没有被关闭释放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在编写一些代码并注意到一个奇怪的问题

这样做。我正在使用为Python 2.5构建的PythonWin 210。我b $ b注意到这个脚本处理的最后一个py文件的问题,

其中有关的tmp文件实际上只写入了

PythonWin关闭了。换句话说,在我运行此脚本之后,生成的tmp文件之一的大小为0kB

。然后我关闭了PythonWin和

然后将其写入。


我猜测垃圾收集器导致文件被写入,

但是不应该关闭吗?


/ Barry


导入操作系统,时间,字符串


dir =''c:\\temp1''


def listAllFile(fileNames,dir,files):

def f1(a,dir = dir):return os.path.join(dir,a)

files2 = map(f1,files)

fileNames.extend( files2)


fileNames = []

os.path.walk(dir,listAllFile,fileNames)


对于fileNames中的fileName:

fileBeginning = os.path.splitext(fileName)[0]

fileEnd = os.path.splitext(fileName)[1]


if fileEnd ==" .py":

print fileName

f = open(fileBeginning +" .tmp",''' w'')

f.write(" Hello")

f.close

解决方案


< bg *** @ yahoo.comwrote in message

news:11 ***************** ****@d55g2000hsg.googlegro ups.com ...


我猜测垃圾收集器导致文件被写入,

但是不应该这样做吗?



只有你打电话才能;)


On Mon,2007年7月30日16:36:00 + 0200,< bg *** @ yahoo.comwrote:





我在这个过程中编写一些代码并注意到一个奇怪的问题

这样做。我正在使用为Python 2.5构建的PythonWin 210。我b $ b注意到这个脚本处理的最后一个py文件的问题,

其中有关的tmp文件实际上只写入了

PythonWin关闭了。换句话说,在我运行此脚本之后,生成的tmp文件之一的大小为0kB

。然后我关闭了PythonWin和

然后将其写入。


我猜测垃圾收集器导致文件被写入,

但是不应该关闭吗?


/ Barry


导入操作系统,时间,字符串


dir =''c:\\temp1''


def listAllFile(fileNames,dir,files):

def f1(a,dir = dir):return os.path.join(dir,a)

files2 = map(f1,files)

fileNames.extend( files2)


fileNames = []

os.path.walk(dir,listAllFile,fileNames)


对于fileNames中的fileName:

fileBeginning = os.path.splitext(fileName)[0]

fileEnd = os.path.splitext(fileName)[1]


if fileEnd ==" .py":

print fileName

f = open(fileBeginning +" .tmp",''' w'')

f.write(" Hello")

f.close



f。 CL ose()


HTH ...

-

python -c" print''''。join( [chr(154 - ord(c))for c in

''U(17zX(%,5.zmz5(17l8(%,5.Z *(93-965

< br>

l7 + - ''])"


Hi,

I''m in the process of writing some code and noticed a strange problem
while doing so. I''m working with PythonWin 210 built for Python 2.5. I
noticed the problem for the last py file processed by this script,
where the concerned tmp file is only actually written to when
PythonWin is closed. In other words, after I run this script, one of
the generated tmp files has a size of 0kB. I then close PythonWin and
it is then written to.

I''m guessing the garbage collector is causing the file to be written,
but shouldn''t close do this?

/Barry

import os, time, string

dir = ''c:\\temp1''

def listAllFile(fileNames,dir,files):
def f1(a,dir=dir): return os.path.join(dir,a)
files2 = map(f1, files)
fileNames.extend(files2)

fileNames = []
os.path.walk(dir,listAllFile,fileNames)

for fileName in fileNames:
fileBeginning = os.path.splitext(fileName)[0]
fileEnd = os.path.splitext(fileName)[1]

if fileEnd == ".py":
print fileName
f=open(fileBeginning+".tmp", ''w'')
f.write("Hello")
f.close

解决方案


<bg***@yahoo.comwrote in message
news:11*********************@d55g2000hsg.googlegro ups.com...

I''m guessing the garbage collector is causing the file to be written,
but shouldn''t close do this?

Only if you call it ;)


On Mon, 30 Jul 2007 16:36:00 +0200, <bg***@yahoo.comwrote:

Hi,

I''m in the process of writing some code and noticed a strange problem
while doing so. I''m working with PythonWin 210 built for Python 2.5. I
noticed the problem for the last py file processed by this script,
where the concerned tmp file is only actually written to when
PythonWin is closed. In other words, after I run this script, one of
the generated tmp files has a size of 0kB. I then close PythonWin and
it is then written to.

I''m guessing the garbage collector is causing the file to be written,
but shouldn''t close do this?

/Barry

import os, time, string

dir = ''c:\\temp1''

def listAllFile(fileNames,dir,files):
def f1(a,dir=dir): return os.path.join(dir,a)
files2 = map(f1, files)
fileNames.extend(files2)

fileNames = []
os.path.walk(dir,listAllFile,fileNames)

for fileName in fileNames:
fileBeginning = os.path.splitext(fileName)[0]
fileEnd = os.path.splitext(fileName)[1]

if fileEnd == ".py":
print fileName
f=open(fileBeginning+".tmp", ''w'')
f.write("Hello")
f.close

f.close()

HTH...
--
python -c "print ''''.join([chr(154 - ord(c)) for c in
''U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965


l7+-''])"


这篇关于文件句柄没有被关闭释放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆