【发布时间】:2018-04-19 01:55:59
【问题描述】:
我有 n 个目录(标记为 0 到 n),每个目录都有一个文件(所有文件都具有相同的名称),我想从中获取每个文件中的某些行。然后我想将这些抓取的行按顺序(从 0 到 n)附加到一个列表中。
这是我的设置:
for i in range(0, nfolders):
folder = "%02d" % i
os.system("cd " + folder)
myFile = open("myOutputFile", "r")
lines = myFile.readlines()
firstLine = float(lines[0])
#I then write a loop to store the next 5 lines in a list using append and call this list nextLines
我的问题是,有没有一种简单的方法可以将所有目录中的 firstLine 附加到一个列表中(我的函数返回),以及将所有目录中的 nextLines 附加到一个列表中(同样,我的函数返回)?
我知道有扩展功能,我可以在这里循环吗(因为假设我有 nfolders = 300,很难手动添加东西)?
谢谢!
【问题讨论】:
-
@jpp - 我正在为你解答,但你删除了你的问题 - 想继续聊天吗? chat.stackoverflow.com/rooms/170642/wwii