简体   繁体   English

使用python make进行写入和写入文件,但不写入或关闭文件

[英]Make and write to file in python makes but doesn't write or close file

I have an interesting issue I'm trying to figure out. 我正在尝试解决一个有趣的问题。 I have a large program and in part of that program I'm making a .xml file and writing to it (just using strings no lxml or anything). 我有一个大型程序,在该程序的一部分中,我正在制作一个.xml文件并写入该文件(仅使用字符串而不使用lxml或其他任何东西)。 I took that specific code out into its own program for testing and am running into an issue. 我将该特定代码带入其自己的程序中进行测试,并且遇到了问题。 The code is 该代码是

import os
directory = 'FileDirectory'
name = 'Test File.xml'

if not os.path.exists('K:\\JOBS\\' + directory + '\\XML Files\\'):
    os.makedirs('K:\\JOBS\\' + directory + '\\XML Files\\')
xmlJob = open('K:\\JOBS\\' + directory + '\\XML Files\\' + name, 'w')
try:
    xmlJob.write('Test write')
except:
    print "Unexpected error:", sys.exc_info()[0]
    raise
xmlJob.close

The code runs through, doesn't give me any errors, the except doesn't trigger, but while the file is made there is nothing written in it. 代码一直运行,不会给我任何错误,除非不会触发,但是在制作文件时没有任何内容写入。 Also I can not delete the file without closing Python first so the .close doesn't seem to be happening either. 另外,我不能在不先关闭Python的情况下删除文件,因此.close似乎也没有发生。 But if I put print statements after the .write and the .close they both trigger so the program supposedly hits all the lines. 但是,如果我将打印语句放在.write和.close之后,它们都会触发,因此该程序可能会碰到所有行。 The above text is the entire program so there's nothing else to be messing with it. 上面的文本是整个程序,因此没有其他麻烦。

Now, when I changed to using with everything works. 现在,当我改为使用一切正常时。 So the code 所以代码

import os
directory = 'FileDirectory'
name = 'Test File.xml'

with open('K:\\JOBS\\' + directory + '\\XML Files\\' + name, 'w') as xmlJob:
    xmlJob.write('Test write')

runs fine. 运行良好。 So obviously I can use the with instead of the open and .close, but at this point in time I want to figure out why the first option failed. 所以很明显我可以使用with代替open和.close,但是在这一点上我想弄清楚为什么第一个选项失败了。 I've used practically the exact same code in other programs to write to log files and such and its worked then. 我实际上已经在其他程序中使用了完全相同的代码来写入日志文件等,然后它开始工作。 Anyone have any ideas? 有人有想法么?

By typing xmlJob.close you are refering to the class method close on the xmlJob object, but not calling it. 通过键入xmlJob.close您将引用xmlJob对象上的类方法close ,但未调用它。

Observe the following: 请注意以下几点:

>>> f = open("tmp.txt")
>>> f
<open file 'tmp.py', mode 'r' at 0x7f906e8c5ae0>
>>> f.close
<built-in method close of file object at 0x7f906e8c5ae0>
>>> f
<open file 'tmp.py', mode 'r' at 0x7f906e8c5ae0>
>>> f.close()
>>> f
<closed file 'tmp.py', mode 'r' at 0x7f906e8c5ae0>

Calling f.close merely prints a repr esentation of that method, and does not call it. 调用f.close只是打印repr该方法的esentation,并且不调用它。

The reason your second block of code works is because the with open(x) as y construct has alot of syntactic sugar behind the scenes . 您的第二段代码起作用的原因是因为with open(x) as y构造的幕后花了 很多 语法糖 Along with exception handling, it also handles the closing of objects cleanly. 除了异常处理,它还可以干净地处理对象的关闭。 Which means you don't need to manually close the file as its closed once the end of the with block is reached. 这意味着,一旦到达with块的末尾,您就无需手动关闭文件,因为文件已关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM