简体   繁体   English

python 在不同文件夹中查找相同文件名,并利用两个文件完成某项任务

[英]Find same file name in different folders, and utilize two files for a certain task in python

There are folders in different folders like below不同文件夹中有文件夹如下

folder 1 - a, b, c...
folder 2 - a, b, c ...

And each folder has files with similar names like below每个文件夹都有类似名称的文件,如下所示

folder 1 - a - one.csv, two.csv,...
folder 2 - a - one_pred.csv, two_pred.csv,...

I would like to check files in each folder and when it finds same folder, go into the folder and when it finds one.csv & one_pred.csv for instance,我想检查每个文件夹中的文件,当它找到相同的文件夹时,go 进入文件夹,当它找到one.csv & one_pred.csv时,例如,

It goes to the ML performance check function它转到 ML 性能检查 function

rms = get_rms(one, one_pred)

But I'm not sure how to loop through two different folder at the same time and pass them to the function.但我不确定如何同时遍历两个不同的文件夹并将它们传递给 function。

I tried我试过了

folders = os.listdir(folder1)
for d in folders:
    files = os.listdir(folder1 + d)
    for f in files:
        if f.split('_')[-1] == 'pred':
            f_truth = f.replace('_pred','')
            rms = get_rms(f_truth, f)

It didn't work.它没有用。 I thought this was wrong before I ran it, but I had no idea what I should do differently...在我运行它之前我认为这是错误的,但我不知道我应该做些什么不同......

Use python's os.walk() to get a list of files in both folders.使用 python 的os.walk()获取两个文件夹中的文件列表。 Then match them as per your needs and run whichever function you want on them.然后根据您的需要匹配它们并运行您想要的任何 function。 Use this and this reference to understand how to use os.walk() .使用这个这个参考来了解如何使用os.walk()

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

相关问题 如何区分两个不同文件夹中具有不同扩展名的同名文件? Python - How to differentiate files with same name with different extension from two different folders? Python 在两个文件夹中查找名称的相同初始部分的文件 - Find files with same initial part of the name in two folders 如何通过Python合并来自同名但位于不同文件夹中的文件的内容? - How to merge content from files with same name but in different folders by Python? 写一个 function 将两个同名文件(文件扩展名不同)移动到 python 同名文件夹中 - Writing a function to move two files with the same name (with different file extensions) to a folder with the same name in python python 以编程方式从不同文件夹导入相同的文件名 - python import the same file name programatically from different folders Python 列出不同文件夹中的某些文件 - Python list certain files in different folders PyCharm 导入不同文件夹中同名文件 - PyCharm Import files with same name in different folders 在python中的多个文件和文件夹中查找作者姓名 - Find author name in multiple files and folders in python 当两个具有相同名称的文件夹导入python - import in python when two folders with same name 在目录中查找重复文件和文件夹并将重复项移动到 python 中的不同文件夹 - Find duplicates files and folders in directory and move the duplicates to different folders in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM