[英]Python compare filename with folder name
I just start python and i have to compare filename with folder name to launch the good sh script.我刚启动 python,我必须将文件名与文件夹名进行比较才能启动好的 sh 脚本。 (i'm using airflow) (我正在使用气流)
import glob
import os
import shutil
from os import path
odsPath = '/apps/data/02_ODS/'
receiptPath = '/apps/data/80_DATA/01_Receipt/'
for files in os.listdir(receiptPath):
if(files.startswith('MEM_ZMII') or files.startswith('FMS') and files.endswith('.csv')):
parsedFiles = files.split('_')
pattern = '_'.join(parsedFiles[0:2])
fileName = '_'.join(parsedFiles[2:5])
fileName = fileName.split('-')[0].lower()
# print('appCode: ', pattern)
# print('fileName: ', fileName)
for odsFolder in os.listdir(odsPath):
if(odsFolder == fileName):
print('it exist: ', str(fileName))
else:
print('it\'s not')
I got 3 files in receiptPath , it only matching for 1 file, but not the others.我在receiptPath 中有3 个文件,它只匹配1 个文件,而不匹配其他文件。 Can someone help me?有人能帮我吗?
Thank a lot!非常感谢!
Ok, your problem is that you overwrite your variable fileName
, so at the end of the first for loop, it only keeps the last value, which is material_makt
.好的,您的问题是您覆盖了变量fileName
,因此在第一个 for 循环结束时,它只保留最后一个值,即material_makt
。 The solution consists in saving all the filenames in a list fileNames_list
, and then you can check if (odsFolder in fileNames_list)
:解决方案包括将所有文件名保存在列表fileNames_list
,然后您可以检查if (odsFolder in fileNames_list)
:
import glob
import os
import shutil
from os import path
odsPath = '/apps/data/02_ODS/'
receiptPath = '/apps/data/80_DATA/01_Receipt/'
fileNames_list = []
for files in os.listdir(receiptPath):
if(files.startswith('MEM_ZMII') or files.startswith('FMS') and files.endswith('.csv')):
parsedFiles = files.split('_')
pattern = '_'.join(parsedFiles[0:2])
fileName = '_'.join(parsedFiles[2:5])
fileName = fileName.split('-')[0].lower()
fileNames_list.append(fileName)
for odsFolder in os.listdir(odsPath):
if (odsFolder in fileNames_list):
print('it exist:', str(odsFolder))
else:
print('it\'s not')
Output :输出 :
it exist: zcormm_familymc
it exist: kpi_obj_data
it exist: material_makt
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.