他为什么给我返回这个答案:TypeError: 'str' object不能被解释为整数

问题描述 投票:0回答:1

`

from os import *
cwd = getcwd()
basename = input('Entrer le nom du fichier')
file = str(cwd + '/' + basename)
if path.isfile(file):
    with open(file, "r") as rf:
        content = f.readline()
        print(content)

我尝试更改导入,但结果相同

python
1个回答
0
投票

from os import *
os.open
导入到全局命名空间中,覆盖内置的
open

os.open
签名:

open(path, flags, mode=511, *, dir_fd=None)

open
签名:

open(
    file,
    mode='r',
    buffering=-1,
    encoding=None,
    errors=None,
    newline=None,
    closefd=True,
    opener=None
)

os.open
的第二个参数是flags(整数)而不是
mode
(字符串)。

修复:

import os  # bad practice to import everything

cwd = os.getcwd()  # use fully qualified method name
basename = input('Enter file name: ')
file = str(cwd + '/' + basename)
if os.path.isfile(file):  # use fully qualified method name
    with open(file, "r") as f:  # "rf" changed to "f" (another bug)
        content = f.readline()
        print(content)
© www.soinside.com 2019 - 2024. All rights reserved.