使用python同时从两个目录中读取同名文件

使用python同时从两个目录中读取同名文件,python,Python,我有目录A A包含A.txt,b.txt,c.txt 我有目录B, B包含a.csv,B.csv,c.csv 我希望能够分别读取和处理文件,如下所示: a.py src = '/home/A' file_paths = [os.path.join(src, f) for f in os.listdir(src)] for file in file_paths: for line in open(file, 'rb').readlines(): #do some some

我有目录A
A
包含
A.txt
b.txt
c.txt

我有目录B
B
包含
a.csv
B.csv
c.csv

我希望能够分别读取和处理文件,如下所示:

a.py

src = '/home/A'
file_paths = [os.path.join(src, f) for f in os.listdir(src)]
for file in file_paths:

    for line in open(file, 'rb').readlines():
    #do some some
    # print some stuff  
src = '/home/B'
file_paths = [os.path.join(src, f) for f in os.listdir(src)]
for file in file_paths:

    with open(filename, 'r') as ff:
    for line in ff:
        _, end, label = line.strip().split('\t')

    #do some some
    # print some stuff  
b.py

src = '/home/A'
file_paths = [os.path.join(src, f) for f in os.listdir(src)]
for file in file_paths:

    for line in open(file, 'rb').readlines():
    #do some some
    # print some stuff  
src = '/home/B'
file_paths = [os.path.join(src, f) for f in os.listdir(src)]
for file in file_paths:

    with open(filename, 'r') as ff:
    for line in ff:
        _, end, label = line.strip().split('\t')

    #do some some
    # print some stuff  
因此,
a.py
的结果如下所示:

file_processed: a.txt   
output    
file processed: b.txt  
output  
file processed: c.txt  
output
同样,从
b.py

file_processed: a.csv   
output    
file processed: b.csv  
output  
file processed: c.csv  
output
我不想执行此过程,因为很难直观地比较.csv和.txt的结果,因为它们位于两个不同的终端或文件上等等

我想做的是:立即从dir读取同名文件,然后执行计算,然后打印结果

示例:我应该从/a中读取a.txt并对其进行处理,然后从/B中读取a.csv并对其进行处理。然后打印两个结果。然后从/A转到b.txt,从/Band转到b.csv,依此类推


请告知。如果您想让我更新
的逻辑,请告诉我做一些事情
代码部分。

只需形成两个文件路径列表,然后同时循环它们

a_root = '/home/A'
b_root = '/home/B'
a_paths = [os.path.join(a_root, f) for f in os.listdir(a_root)]
b_paths = [os.path.join(b_root, f) for f in os.listdir(b_root)]
for a_path, b_path in zip(a_paths, b_paths):
    with open(a_path) as a_file, open(b_path) as b_file:
        a_data = a_file.read()
        b_data = b_file.read()
    # do stuff with a and b

只需形成两个文件路径列表,然后同时循环它们

a_root = '/home/A'
b_root = '/home/B'
a_paths = [os.path.join(a_root, f) for f in os.listdir(a_root)]
b_paths = [os.path.join(b_root, f) for f in os.listdir(b_root)]
for a_path, b_path in zip(a_paths, b_paths):
    with open(a_path) as a_file, open(b_path) as b_file:
        a_data = a_file.read()
        b_data = b_file.read()
    # do stuff with a and b
示例:我应该从/a中读取a.txt并对其进行处理,然后读取 a、 csv from/B并对其进行处理。然后打印两个结果。然后转到 b、 txt来自/A,b.csv来自/Band,依此类推

我觉得你已经回答了你自己的问题。这甚至可以用作伪代码。这里有一个不雅但有效的例子

filenames = [f for f in however_you_scrape_you_filenames]
directory_extension_map = {'/path/to/a': 'txt',
                           '/path/to/b': 'csv'
                          }
for fname in filenames:
    for directory in directory_extension_map:
        extension = directory_extension_map[directory]
        file_path = os.path.join(directory, f'{fname}.{extension}')
        # Read and parse file_path
我觉得你可以从这里开始

示例:我应该从/a中读取a.txt并对其进行处理,然后读取 a、 csv from/B并对其进行处理。然后打印两个结果。然后转到 b、 txt来自/A,b.csv来自/Band,依此类推

我觉得你已经回答了你自己的问题。这甚至可以用作伪代码。这里有一个不雅但有效的例子

filenames = [f for f in however_you_scrape_you_filenames]
directory_extension_map = {'/path/to/a': 'txt',
                           '/path/to/b': 'csv'
                          }
for fname in filenames:
    for directory in directory_extension_map:
        extension = directory_extension_map[directory]
        file_path = os.path.join(directory, f'{fname}.{extension}')
        # Read and parse file_path

我觉得你可以从这里接手。

请提供一份报告。另外,为什么要在Python代码中使用C风格的注释?使用python注释。很抱歉,我将更改该注释。请提供一个。另外,为什么要在Python代码中使用C风格的注释?使用python注释。很抱歉,我会更改它。进展如何?这回答了你的问题吗?进展如何?这回答了你的问题吗?