简体   繁体   English

使用python同时从两个目录读取具有相同名称的文件

[英]read files with same name from two directories simultaneously using python

I have directory A . 我有目录A。
A contains a.txt , b.txt , c.txt . A包含a.txtb.txtc.txt

I have directory B , 我有目录B
B contains a.csv , b.csv , c.csv . B包含a.csvb.csvc.csv

I want am able to read and process files both separately as follows: 我希望能够分别读取和处理文件,如下所示:

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  

b.py

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  

So results from a.py look like : 因此,来自a.py结果如下所示:

file_processed: a.txt   
output    
file processed: b.txt  
output  
file processed: c.txt  
output

and similarly from b.py 同样来自b.py

file_processed: a.csv   
output    
file processed: b.csv  
output  
file processed: c.csv  
output

I don't want to do this ^ process as it is hard to visually compare results of a.csv with a.txt because they are on two different terminals or files and so on. 我不想执行此^过程,因为很难直观地比较a.csv和a.txt的结果,因为它们位于两个不同的终端或文件等上。

What I want to do is :read files with same name from dir's at once and then perform computations and then print the results 我想要做的是:一次从目录中读取具有相同名称的文件,然后执行计算,然后打印结果

Example: I should read a.txt from /A and process it , and then read a.csv from /B and process it. 示例:我应该从/ A读取a.txt并对其进行处理,然后从/ B读取a.csv并对其进行处理。 then print both results. 然后打印两个结果。 Then move on to b.txt from /A and b.csv from /Band so on. 然后从/ A移至b.txt,从/ Band移至b.csv,依此类推。

Please advise. 请指教。 Let me know if you want me to update the logic for do some stuff portion of the code. 让我知道您是否要更新逻辑以do some stuff代码的do some stuff部分。

Just form both lists of filepaths then loop over them at the same time. 只需形成两个文件路径列表,然后同时遍历它们即可。

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

Example: I should read a.txt from /A and process it , and then read a.csv from /B and process it. 示例:我应该从/ A读取a.txt并对其进行处理,然后从/ B读取a.csv并对其进行处理。 then print both results. 然后打印两个结果。 Then move on to b.txt from /A and b.csv from /Band so on. 然后从/ A移至b.txt,从/ Band移至b.csv,依此类推。

I feel like you have already answered your own question. 我觉得您已经回答了自己的问题。 This even works as pseudocode. 这甚至可以用作伪代码。 Here is an inelegant but effective example. 这是一个优雅但有效的例子。

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

I feel like you can take it from here. 我觉得您可以从这里拿走它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM