简体   繁体   English

python def,打印返回值

[英]python def, print returned value

I am trying to get filenames that are written line by line in a text file. 我试图获取在文本文件中逐行写入的文件名。 I have tried using the following code to return a name for each line in a variable called filenames. 我尝试使用以下代码为名为filenames的变量中的每一行返回一个名称。 When I try to print the filenames variable to assure it has been properly retrieved, an error occurs stating the (filenames) is not defined. 当我尝试打印文件名变量以确保它已被正确检索时,会发生错误,指出未定义(文件名)。 I also made a test variable to check if it is just not reading the lines correctly but the same error occurs. 我还做了一个测试变量来检查它是否只是没有正确读取行,但是发生了同样的错误。 Any ideas? 有任何想法吗?

def read(textFile):
    filenames = []
    test = "test"
    with open ("C:\\xampp\\htdocs\\test.new\\multiUploadTest\\var.txt", "r+") as myfile:
        for line in myfile:
            filenames.append(line)
            print test
            print (filenames)
    return (filenames, test)

print (test)
print (filenames)

You need to do something with the values you return , eg: 您需要对return的值执行某些操作 ,例如:

test, filenames = read("testing.txt")
print(test)
print(filenames)

Names in Python are just references to objects; Python中的名称只是对象的引用; returning test and filenames passes the objects those names reference back to the calling function, but doesn't mean that the same names will automatically be available. 返回testfilenames 名将这些名称引用对象传递回调用函数,但并不意味着相同的名称将自动可用。 In fact, you can name them something completely different in the calling function and everything will still work: 事实上,你可以在调用函数中将它们命名为完全不同的东西,一切都会起作用:

foo, bar = read("testing.txt")
print(foo)
print(bar)

test and filenames are only available inside your function. testfilenames仅在您的函数中可用。 so you need to either call the function and assign the values to variable outside the function and then use them, or you could declare them as global in which case you don't need to return them. 所以你需要调用函数并将值赋给函数外部的变量然后使用它们,或者你可以将它们声明为全局,在这种情况下你不需要返回它们。

So, you could either do this: 所以,你可以这样做:

def read(textFile):
    filenames = []
    test = "test"
    with open ("C:\\xampp\\htdocs\\test.new\\multiUploadTest\\var.txt", "r+") as myfile:
        for line in myfile:
            filenames.append(line)
            print test
            print (filenames)
    return (filenames, test)

filenames, test = read(your_filename)
print filenames, test

or you could the following: 或者您可以:

def read(textFile):
    global filenames, test
    filenames = []
    test = "test"
    with open ("C:\\xampp\\htdocs\\test.new\\multiUploadTest\\var.txt", "r+") as myfile:
        for line in myfile:
            filenames.append(line)
            print test
            print (filenames)

read(your_filename)
print filenames, test

You are getting that error "C:\\xampp\\htdocs\\test.new\\multiUploadTest\\var.txt", "r+" - because of the double \\ - so the file can not be read. 您收到错误“C:\\ xampp \\ htdocs \\ test.new \\ multiUploadTest \\ var.txt”,“r +” - 因为双\\ - 因此无法读取文件。

This error has nothing to do with the code it has to do with file location, do a test, copy the file in the same folder as the code and then point the with open() to that folder, or just drop the \\ and use a single \\ 这个错误与它与文件位置有关的代码无关,做一个测试,将文件复制到与代码相同的文件夹中,然后用open()指向该文件夹,或者只是删除\\并使用一个\\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM