擅长:python、mysql、java
<p>我不确定我是否理解您提到的问题,但这里有一个片段,可以为您提供要处理的文件列表。您可以用更聪明或可读的方式更改筛选,但我已经用一个级别的嵌套文件夹对此进行了测试,它完成了这项工作。你可以从这里开始写一些更清晰或更适合你需要的东西</p>
<pre><code>#parent_dir=os.getcwd()+"\\temp"
files_to_read=[]
walk = [(subdir, dirs, files) for (subdir, dirs, files) in os.walk(parent_dir) if not (subdir==parent_dir)] #Skip the root directory
for (subdir, dirs, files) in walk:
file_to_read = list(filter(lambda x: "ROH.csv" in x or "RRS.csv" in x, files)) #Explicitly filter for one of the two strings
if len(file_to_read)>1:
file_to_read = list(filter(lambda x: "ROH.csv" in x, files))[0] #explictly pick the ROH file if there are both files
elif len(file_to_read)>0:
file_to_read = file_to_read[0] #Otherwise pick the only file in the list i.e. RRS
file_to_read=subdir+os.path.sep+file_to_read
files_to_read.append(file_to_read)
</code></pre>