Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python列表仍在作用域中时丢失值_Python_List_Scope - Fatal编程技术网

Python列表仍在作用域中时丢失值

Python列表仍在作用域中时丢失值,python,list,scope,Python,List,Scope,请看以下用python编写的代码片段 import os my_files = [] FOLDER_PATH = r'PATH_To_FOLDER' for root, dirs, files in os.walk(FOLDER_PATH): #print(root) my_files = [root +'\\'+ f for f in files if f.endswith('.txt')] print(my_files[:]) #files are there in

请看以下用python编写的代码片段

import os

my_files = []
FOLDER_PATH = r'PATH_To_FOLDER'
for root, dirs, files in os.walk(FOLDER_PATH):
    #print(root)
    my_files = [root +'\\'+ f for f in files if f.endswith('.txt')]
    print(my_files[:]) #files are there in my_files
    #print(len(my_files)) #outputs 2

for f in my_files: #len(my_files) = 0 at this point, why?
    with open(f,'r') as ff:
    print(ff.read())
虽然my_文件显然仍在作用域中,但它应该保留其值。但事实并非如此?

您需要使用list.extend将一个列表的内容添加到另一个列表中

目前,您只是在每次迭代时重新分配给相同的列表变量,因此如果它为空,则只意味着os.walk的最后一次迭代没有返回任何文件。如果您定期更改它所指的内容,那么它是否在范围内这一事实没有任何区别

from os import walk, path

my_files = []
FOLDER_PATH = r'.'
for root, dirs, files in walk(FOLDER_PATH):
    my_files.extend(path.join(root, f) for f in files if f.endswith('.txt'))

print(my_files)

for f in my_files:
    print('**** %s' % f)
    with open(f,'r') as ff:
        print(ff.read())

另外,使用os.path.join将为您提供独立于平台的路径创建。

列表理解将创建一个新列表,您可以重新分配变量以处理该新列表。这非常像:

>>> a = [1, 2, 3]
>>> a = [4, 5, 6]
并期望:

>>> a
[1, 2, 3, 4, 5, 6]
相反,您希望使用extend方法或+=,将所有新值追加到现有列表中:

在您的例子中,看起来是这样的,注意您可以从列表理解中删除[],将其转换为生成器表达式,这在本例中没有什么区别,只是为了使代码更具可读性:

my_files.extend(root +'\\'+ f for f in files if f.endswith('.txt'))
使用list.extend

您正在做的是在您的案例中重新分配“列出我的文件”列表。因此,在每个循环中,它的值都被重新分配,而不是添加到现有列表中。这就是list.extend的来源

my_files = []
    FOLDER_PATH = r'abc'
    for root, dirs, files in os.walk(FOLDER_PATH):
        my_files.extend(root +'/'+ f for f in files if f.endswith('.txt'))

    for f in my_files: #len(my_files) = 0 at this point, why?
        with open(f,'r') as ff:
            print(ff.read())

但在每次迭代中,您都会将其分配给一个新列表,也许您想使用list.extend?如果最后的迭代产生一个空列表,可以解释为什么它是空的。如果Paul的解释是正确的,这可以通过简单地将你的=更改为a+=来解决。你不需要理解列表;删除[and]并使其成为生成器表达式。请注意,将原始表达式中的=替换为+=也会起作用。@MartijnPieters感谢这是一个值得的更改,因为我们不知道可能有多少文件。
my_files = []
    FOLDER_PATH = r'abc'
    for root, dirs, files in os.walk(FOLDER_PATH):
        my_files.extend(root +'/'+ f for f in files if f.endswith('.txt'))

    for f in my_files: #len(my_files) = 0 at this point, why?
        with open(f,'r') as ff:
            print(ff.read())