迭代python中的大型文件目录系统,获取太多值来解压缩

时间:2013-05-31 19:46:32

标签: python file directory

我在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,os.walk(base_directory)中的文件”感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

你应该打开3元组的包装

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

例如

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