Python 3.x 我如何消除“错误”;名称错误“;当关闭我在另一个函数中打开的文件时?

Python 3.x 我如何消除“错误”;名称错误“;当关闭我在另一个函数中打开的文件时?,python-3.x,function,fclose,Python 3.x,Function,Fclose,W_A1112000-02,移动平均线,59.666667,50.92582302,68.40751031,受伤,人数,攻击,验证,整体流行,所有年龄,致命 def append_to_datalist(): #Datalist should be called append_to_datafile0, #change that for the next program """Append_to_datalist (shoul

W_A1112000-02,移动平均线,59.666667,50.92582302,68.40751031,受伤,人数,攻击,验证,整体流行,所有年龄,致命

def append_to_datalist(): #Datalist should be called append_to_datafile0, 
                          #change that for the next program

       """Append_to_datalist (should be datafile) first wipes the outfile 
          clean then appends all read lines containing the
          same year specified in querydate() from the infile to the 
          outfile"""

    outfile = open("datalist.csv", "w") #these two lines are for resetting 
                                         #the file so it remains manageably 
                                         #small
    outfile.write('')                   #this is the second line
    outfile = open("datalist.csv", "a")
    next(infile)
# extract data
    for line in infile:
        linefromfile = line.strip('\n').split(',')
        tuple1 = tuple(linefromfile)
        outfile.write('\n' + str(tuple1))
    outfile.close()

def openfile_and_append_to_datalist():
    # input for file name
    filename = input(
    "Please enter the name of the file, including the file extension, from 
     which we will be extracting data"
    " ex)injury_statistics.txt ")

    # open infile
    infile = open(filename, "r")

    # append infile data to outfile
    append_to_datalist()

    # close infile
    infile.close()

openfile_and_append_to_datalist()
W_A1112001-03,移动平均线,60,51.23477459,68.76522541,伤害,数量,攻击,验证,整体流行,所有年龄,致命

def append_to_datalist(): #Datalist should be called append_to_datafile0, 
                          #change that for the next program

       """Append_to_datalist (should be datafile) first wipes the outfile 
          clean then appends all read lines containing the
          same year specified in querydate() from the infile to the 
          outfile"""

    outfile = open("datalist.csv", "w") #these two lines are for resetting 
                                         #the file so it remains manageably 
                                         #small
    outfile.write('')                   #this is the second line
    outfile = open("datalist.csv", "a")
    next(infile)
# extract data
    for line in infile:
        linefromfile = line.strip('\n').split(',')
        tuple1 = tuple(linefromfile)
        outfile.write('\n' + str(tuple1))
    outfile.close()

def openfile_and_append_to_datalist():
    # input for file name
    filename = input(
    "Please enter the name of the file, including the file extension, from 
     which we will be extracting data"
    " ex)injury_statistics.txt ")

    # open infile
    infile = open(filename, "r")

    # append infile data to outfile
    append_to_datalist()

    # close infile
    infile.close()

openfile_and_append_to_datalist()
W_A1112002-04,移动平均线,59,50.30812505,67.69187495,伤害,数量,攻击,验证,整体流行,所有年龄,致命

def append_to_datalist(): #Datalist should be called append_to_datafile0, 
                          #change that for the next program

       """Append_to_datalist (should be datafile) first wipes the outfile 
          clean then appends all read lines containing the
          same year specified in querydate() from the infile to the 
          outfile"""

    outfile = open("datalist.csv", "w") #these two lines are for resetting 
                                         #the file so it remains manageably 
                                         #small
    outfile.write('')                   #this is the second line
    outfile = open("datalist.csv", "a")
    next(infile)
# extract data
    for line in infile:
        linefromfile = line.strip('\n').split(',')
        tuple1 = tuple(linefromfile)
        outfile.write('\n' + str(tuple1))
    outfile.close()

def openfile_and_append_to_datalist():
    # input for file name
    filename = input(
    "Please enter the name of the file, including the file extension, from 
     which we will be extracting data"
    " ex)injury_statistics.txt ")

    # open infile
    infile = open(filename, "r")

    # append infile data to outfile
    append_to_datalist()

    # close infile
    infile.close()

openfile_and_append_to_datalist()
当我运行此文件时,它会正常运行,直到它尝试关闭内嵌,然后返回“名称错误'infle'未定义”

我不确定除了从openfile_和我尝试失败的_append_to_datalist()中取消测试append_to_datalist()之外,还要尝试什么


我的问题之所以说infle在另一个函数中是打开的,是因为append_to_datalist()使用infle。

问题似乎不是关闭
infle
,而是在
append_to_datalist()函数中使用它。
namererror
异常告诉您没有定义
infle
,这是正确的,因为在该函数中,它没有定义。它仅在
openfile\u和\u append\u to\u datalist()
的范围内定义

要从
append_to_datalist()
引用
infle
,需要将其作为函数参数传递。首先更改函数定义:

def append_to_datalist(infile):
    ...
然后在调用函数时传递
infle

infile = open(filename, "r")
append_to_datalist(infile)
infile.close()

请修复您的代码格式(一些缩进看起来是错误的),并将其缩减为您的问题的代表性示例。一个简单的开始是删除注释代码,并删除
querydate
函数,该函数在代码的其他地方没有调用。越小越好。这将帮助人们成功地回答您的问题。querydate由附加到datalist的forloop使用。我现在将编辑代码。谢谢!我看到注释中提到的
querydate
,但我仍然找不到代码中的任何用法。如果调用函数“querydate()”并在函数中使用“querydate”,则函数返回和地址引用。我称之为“qdate”是因为代码后面引用它。我的意思是,您的问题涉及
openfile\u和\u append\u to\u datalist()
,以及
append\u to\u datalist()
querydate()
函数可能是程序的一部分,但在您的示例中没有使用,并且似乎与您的问题无关,这与
infle
有关。