gpt4 book ai didi

python - 在Python中迭代大型文件目录系统,获取太多值而无法解压

转载 作者:行者123 更新时间:2023-12-01 05:43:28 26 4
gpt4 key购买 nike

我正在 python 中迭代一个非常大的文件和目录系统。我从基本目录开始,并且我知道该目录的深度不超过基本目录的 1 个文件夹。我正在尝试使用如下代码读出 base_directories 子文件夹中每个文件的第二行:

a_list = []
for dir, files in os.walk(base_directory):
for file in files:
f=open(file, 'r')
for line in f:
#reads in the second line
if line==1:
a_list.append(line)
break
f.close()

但是我得到了太多的值,无法在“for dir, file in os.walk(base_directory)”这一行专门解压,谢谢您的帮助!

最佳答案

您应该解压一个三元组

walk(top, topdown=True, onerror=None, followlinks=False)    Directory tree generator.    For each directory in the directory tree rooted at top (including top    itself, but excluding '.' and '..'), yields a 3-tuple        dirpath, dirnames, filenames

eg.

for dir, dirnames, files in os.walk(base_directory):

此外,这也行不通。 line 是一个适合初学者的字符串

  for line in f:
#reads in the second line
if line==1:
a_list.append(line)
break

你可以把它改成这样

  for i, line in enumerate(f):
#reads in the second line
if i==1:
a_list.append(line)
break

但我认为这是一个更简单的方法

  import os
a_list = []
for dirpath, dirnames, files in os.walk(base_directory):
for file in files:
with open(os.path.join(dirpath, file), 'r') as f:
next(f) # skip the first line
a_list.append(next(f)) # store the second line

关于python - 在Python中迭代大型文件目录系统,获取太多值而无法解压,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864778/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com