Python - 不写入文件?

时间:2017-05-07 14:50:39

标签: python file

我正在尝试写一个这样的文件:

debugfile = open("file.txt", "w")
debugfile.write("%i" % (feature['properties']['cellId']))
debugfile.close()

这里feature['properties']['cellId']是一个整数。该文件已创建,但保持为空。我错过了什么?

更新 我试着写信给控制台。

debugfile = open("file.txt", "w")
print(feature['properties']['cellId'])
debugfile.write("%i" % (feature['properties']['cellId']))
debugfile.close()

这显示了控制台: enter image description here 该文件仍为空。

2 个答案:

答案 0 :(得分:1)

通过打印feature['properties']['cellId']检查值。

如果我初始化feature

feature = {'properties':{'cellId':1}}

代码有效。所以代码没有错。

debugfile = open("file.txt", "w")
debugfile.write("%i" % (feature['properties']['cellId']))
debugfile.close()

答案 1 :(得分:-2)

你是不是想写一个tuple

没关系,这种方式有效:

#!python
#coding=utf-8

import sys
print(sys.version)

debugfile = open("file1.txt", "w")
debugfile.write("%i" % (123))
debugfile.close()

debugfile = open("file2.txt", "w")
debugfile.write("%i" % 123)
debugfile.close()