【问题标题】:interger required to open file python [duplicate]打开文件python所需的整数[重复]
【发布时间】:2019-01-20 02:45:54
【问题描述】:

好的,所以我需要通过创建/打开(如果文件存在)对程序进行文件测试,并在其中附加一些文本以确认它是否有效。在我开始在测试中添加模块之前,测试非常顺利,但是一旦我完成了模块的安装并开始使用Tkinter 模块的 GUI 工作,现在我得到一个需要整数的错误。有一篇关于它的帖子,但那里的人只是用readline() 搞砸了一些事情,这对我来说不是这样。我认为这与JSON 模块或我在文件测试后添加到#!/usr/bin/env python 的事实有关,但我不知道。

这是我的代码

#!/usr/bin/env python

#shit to import
from json import *
from os import *
from sys import *
from numpy import *
from datetime import *
from tkinter import *

#testing
with open("test.txt","a") as file:
    file.read()
    file.write("The test has been completed \n")

#GUI
win = Tk()
win.mainloop()

【问题讨论】:

  • 使用file 作为变量名不是一个好习惯。考虑切换到不同的变量名,例如inFile。关于您的错误,它与 from os import * 行有关,因为它在另一个问题 here 中提到。
  • open 曾经很好。 os.open 是另一回事,它希望 flags 作为第二个参数(必须是整数)。 from os import *open 替换为 os.open。我强烈建议不要使用from stuff import *,因为你永远不知道什么会被覆盖。
  • @VasilisG。为什么? file 不是 Python 3 中的内置函数(它们没有隐藏任何东西); from blah import * 更具概率性和更糟糕的风格
  • @Chris_Rands 你是绝对正确的。那是我的错误。

标签: python python-3.x file module


【解决方案1】:

这是因为你正在做 import * 的东西,而内置的 open() 函数被 os.open() 替换。

使用import * 的东西是个坏主意。只需导入您真正需要的东西,您就会很高兴;)

【讨论】:

    猜你喜欢
    • 2016-07-20
    • 2017-12-01
    • 2012-05-23
    • 1970-01-01
    • 2015-05-29
    • 2018-10-22
    • 2012-11-28
    • 2018-02-10
    • 2012-03-26
    相关资源
    最近更新 更多