简体   繁体   English

Python 比较文件名和文件夹名

[英]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.

相关问题 蟒蛇; 如何替换字符串模式并保存到文件中,如何使用字符串变量将文件名从文件夹名重命名为文件名? - Python; how to replace string pattern and save to a file, rename the file by string variable from folder name to the filename? 遍历文件夹,提取不带扩展名的文件名分配为Python中的API变量名 - Iterate through folder, extracting filename without extension assign as API variable name in Python Python 将文件复制到新目标时,以父文件夹名称作为前缀文件名 - Python prefix filename with parent folder name while copying files to a new destination 使用 python 重命名文件夹中的文件名列表 - Rename a list of filename in a folder using python 在python中从数据框中提取文件夹和文件名 - extracting folder and filename from dataframe in python 使用正则表达式搜索文件名和父文件夹 - Python - Search for Filename and Parent Folder Using Regex - Python Python 将具有特定文件名的文件排序到特定文件夹 - Python Sort files with particular filename into specific folder 在python中递归返回文件夹路径和文件名 - Return Folder path and filename recursively in python ImportError-名称冲突,父文件夹与另一个文件名具有相同的名称 - ImportError - Name conflict, parent folder has the same name as another filename Python:比较两个文件夹并将具有相同名称的文件从1个文件夹移动到另一个文件夹 - Python : Compare two folders and move files with the same name from 1 folder to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM