如何使用模式从不同目录读取不同文件
How to read different files from different directories with pattern
我有目录结构:
--- main_dir
------ aaa
--------subaaa
----------file
------ xxx
--------subxxx
----------file
------ 111
--------sub111
----------file
等等
所有目录名称不同,但 FILE 名称相同。
每个文件都有三行。
我的问题是如何以最快的方式从每个文件中读取这三行?
我编写了搜索文件的代码。但仍然不知道如何阅读所有这些。看看这个:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
return r, print(r)
list_files('some_path)
我想实现这样的目标:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
return r, print(r)
with open(r[*]) as f: #this is the question
f.readlines() #how to read all of the files
list_files('some_path)
考虑使用:
import os
listOfFiles = os.listdir("the absolute path to main_dir")
for fileName in listOfFiles:
print fileName
# read the lines of fileName
with open(fileName) as f:
contentOfFile = f.readlines()
我找到了打开 FILE 的解决方案,代码如下:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
with open(os.path.join(root, name)) as f:
print(f.readlines())
return r, print(r)
list_files('C:\gpdw-feeds')
现在我的结构有问题。
如何仅列出最后一个子文件夹中的文件?
示例:
---main_dir
----sub
-----file1
----sub_sub
-----file2
我只想读取文件 2,但我的代码会读取所有子目录中的所有文件。
要有效地找到每个名为 1.txt 的文件并打印其以文件名为前缀的内容,只需:
find . -name '1.txt' -exec awk '{print FILENAME, [=10=]}' {} +
如果这不是您想要的,请编辑您的问题以进行澄清。
我有目录结构:
--- main_dir
------ aaa
--------subaaa
----------file
------ xxx
--------subxxx
----------file
------ 111
--------sub111
----------file
等等
所有目录名称不同,但 FILE 名称相同。 每个文件都有三行。 我的问题是如何以最快的方式从每个文件中读取这三行?
我编写了搜索文件的代码。但仍然不知道如何阅读所有这些。看看这个:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
return r, print(r)
list_files('some_path)
我想实现这样的目标:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
return r, print(r)
with open(r[*]) as f: #this is the question
f.readlines() #how to read all of the files
list_files('some_path)
考虑使用:
import os
listOfFiles = os.listdir("the absolute path to main_dir")
for fileName in listOfFiles:
print fileName
# read the lines of fileName
with open(fileName) as f:
contentOfFile = f.readlines()
我找到了打开 FILE 的解决方案,代码如下:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
with open(os.path.join(root, name)) as f:
print(f.readlines())
return r, print(r)
list_files('C:\gpdw-feeds')
现在我的结构有问题。 如何仅列出最后一个子文件夹中的文件? 示例:
---main_dir
----sub
-----file1
----sub_sub
-----file2
我只想读取文件 2,但我的代码会读取所有子目录中的所有文件。
要有效地找到每个名为 1.txt 的文件并打印其以文件名为前缀的内容,只需:
find . -name '1.txt' -exec awk '{print FILENAME, [=10=]}' {} +
如果这不是您想要的,请编辑您的问题以进行澄清。