Python 使用函数将input()写入文件并读取文件名错误
我正在尝试创建一个程序,它接受用户输入并将其保存到文本文件中,每次都添加到文档中,而不覆盖以前的条目 尝试运行该程序时,出现以下错误: 文件。写入(文件,a,b,c,d) NameError:未定义名称“a” 我在Python 使用函数将input()写入文件并读取文件名错误,python,file,Python,File,我正在尝试创建一个程序,它接受用户输入并将其保存到文本文件中,每次都添加到文档中,而不覆盖以前的条目 尝试运行该程序时,出现以下错误: 文件。写入(文件,a,b,c,d) NameError:未定义名称“a” 我在def how()中定义了一个。为什么变量“a”不可用于def write() 变量a、b、c和d是函数how()和write()的局部变量,因此write()不知道要写什么 返回how()中的值: 调用how()时获取这些值: 并在write()中使用它们: 并在调用write时传递
def how()
中定义了一个。为什么变量“a”不可用于def write()
变量
a
、b
、c
和d
是函数how()
和write()
的局部变量,因此write()
不知道要写什么
返回how()
中的值:
调用how()
时获取这些值:
并在write()
中使用它们:
并在调用write
时传递参数:
def write(a, b, c, d):
files = open("diary.txt","w")
files.write(file, a, b, c, d)
file.close()
write(a, b, c, d)
你的问题不清楚,但我猜
diary.txt
没有你期望的内容。
看一看,上面写着:
“w”打开以进行写入,首先截断文件
因此,每次尝试写入时,文件都会被截断。
但这些文件也说:
“a”打开进行写入,如果文件存在,则追加到文件末尾
因此,请尝试使用以下方法打开文件:
open('diary.txt', 'wa')
how()
函数需要返回a、b、c、d
值:
def how():
print (time.strftime("%I:%M:%S"))
print (time.strftime("%d/%m/%Y"))
a = input("How are you today?: ")
b = input("What things happened today?: ")
c = input("What was the main happening of the day? Was it good or bad?: ")
if "good" in c:
print("Good? That's great.")
else:
d = input("I'm sorry it wasn't good. How can I help?: ")
return a,b,c,d
让他们:
a,b,c,d = how()
当您编写它们时,发送它们:
write(a,b,c,d)
然后写文件。请小心,您的代码中有一个错误
def write(a,b,c,d):
file = open("diary.txt","wa")
file.write(a)
file.write(b)
file.write(c)
file.write(d)
file.close()
这管用
import time
a = ""
b = ""
c = ""
d = ""
prevFile = ""
def how():
print (time.strftime("%I:%M:%S"))
print (time.strftime("%d/%m/%Y"))
global a
global b
global c
global d
a = input("How are you today?: ")
b = input("What things happened today?: ")
c = input("What was the main happening of the day? Was it good or bad?: ")
if "good" in c:
print("Good? That's great.")
else:
d = input("I'm sorry it wasn't good. How can I help?: ")
def check():
file = open("diary.txt","w")
file.write("---")
file.close()
def open_file():
global prevFile
file = open("diary.txt","r")
for line in file:
prevFile += line
file.close()
def writeFile():
files = open("diary.txt","w")
files.write(prevFile)
files.write(a + "\n")
files.write(b + "\n")
files.write(c + "\n")
files.write(d + "\n\n")
files.close()
how()
# check()
open_file()
writeFile()
要每次添加而不覆盖,您需要以“a”(追加)模式打开文件:
另外,现在您要关闭文件两次,但不关闭任何地方的文件。这样打开文件是一个好习惯:
with open(file, 'a') as diary:
diary.write(b)
diary.write(c)
这样,在您离开with块后,即使with块中出现异常,文件也会自动关闭。什么意思您不能让它工作?你需要对问题给出更清楚的解释,这样它才有效?你不应该将你的问题编辑成完全不同的内容,因为这会使当前的答案无效。我尝试了你的解决方案,但出现了以下错误-TypeError:write()缺少4个必需的位置参数:“a”、“b”、“c”和“d”@JohnH请检查我的答案。它解决了此问题。仍然不工作-name错误:名称“a”不可用defined@JohnH我的错。我没有正确缩进返回值
。谢谢。我将研究代码以及它在这里工作的原因。
def write(a,b,c,d):
file = open("diary.txt","wa")
file.write(a)
file.write(b)
file.write(c)
file.write(d)
file.close()
import time
a = ""
b = ""
c = ""
d = ""
prevFile = ""
def how():
print (time.strftime("%I:%M:%S"))
print (time.strftime("%d/%m/%Y"))
global a
global b
global c
global d
a = input("How are you today?: ")
b = input("What things happened today?: ")
c = input("What was the main happening of the day? Was it good or bad?: ")
if "good" in c:
print("Good? That's great.")
else:
d = input("I'm sorry it wasn't good. How can I help?: ")
def check():
file = open("diary.txt","w")
file.write("---")
file.close()
def open_file():
global prevFile
file = open("diary.txt","r")
for line in file:
prevFile += line
file.close()
def writeFile():
files = open("diary.txt","w")
files.write(prevFile)
files.write(a + "\n")
files.write(b + "\n")
files.write(c + "\n")
files.write(d + "\n\n")
files.close()
how()
# check()
open_file()
writeFile()
open(file, 'a')
with open(file, 'a') as diary:
diary.write(b)
diary.write(c)