`
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)
我尝试更改导入,但结果相同
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)