首页 > 解决方案 > 将文件重定向到另一个位置,以防 Python 中的 IF 条件为 TRUE

问题描述

我有一个 python 代码,它读取一个嵌套的 JSON 文件并根据下面代码中的 FUNCTIONS(例如源、运算符和目标)的条件返回 TRUE 或 FALSE - 我能够得到它

现在,我想为函数(源、操作员和目标)设置一个条件,如果它满足 IF 条件 >> 那么它应该重定向它通过所有验证的文件名(源、操作员、目标)并移动文件名到另一个位置

即,例如

def source():
            if sourceName_check:
                print("sourceName is vaild and starts with ES_:", sourceName_check) ---->>O/P TRUE >> pick that file and store in different directory/folder
            else:
                print("sourceName not starting with ES_: ", sourceName_check)

输出:如果条件为真——那么 >> 打印文件名并 >>> 存储在另一个目录中

我正在读取所有 JSON 文件并作为输入(即 .JSON)传递并传递给所有函数。所有文件的所有功能都将被迭代

以下是整个代码 - 请帮助获取我的解决方案。谢谢!################################################# ##########################

#!/bin/python
import os, json
import sys

path_to_json = "C:/Users/lm989970/Downloads/flows"


def main():
    for file_json in os.listdir(path_to_json):
        if file_json.endswith('.json'):
            print("""""######starting######""")
            print(file_json)
            try:
                 with open("{}/{}".format(path_to_json,file_json), 'r') as json_file:
                    json_data = json.load(json_file)

            except Exception as e  :
                print (e)
                print("JSON File is Invalid")

            else:
                print("JSON File is Valid")
                ReadFile = json_data['flow']
                get_status(ReadFile)
                source(ReadFile)
                operators(ReadFile)
                target(ReadFile)


def get_status(ReadFile):
    for index in range(0, len(ReadFile['flowVersions'])):
        ReadStatus = ReadFile['flowVersions'][index]["status"]
        print("status is : ", ReadStatus)


def source(ReadFile):
    for index in range(0, len(ReadFile['flowVersions'])):
        for i in range(0, len(ReadFile['flowVersions'][index]['topics'])):
            ReadSource = ReadFile['flowVersions'][index]["topics"][i]['name']
            print('sourceName is : ', ReadSource)
            sourceName_check = ReadSource.startswith('ES_')

            if sourceName_check:
                print("sourceName is vaild and starts with ES_:", sourceName_check)
            else:
                print("sourceName is not starting with ES_:", sourceName_check)


def operators(ReadFile):
    for index in range(0, len(ReadFile['flowVersions'])):
        for i in range(0, len(ReadFile['flowVersions'][index]['operators'])):
            ReadOperators = ReadFile['flowVersions'][index]["operators"][i]['name']
            print('Operators are : ', ReadOperators)
            operatorCheck = ReadOperators.startswith(("TRNS_", "FIL", "LKP_", "AGG_", "SSP_", "PAG_"))

            if operatorCheck:
                print("operator is vaild and starts with:", operatorCheck)
            else:
                print("operator is not starting with TRNS_:", operatorCheck)



def target(ReadFile):
    for index in range(0, len(ReadFile['flowVersions'])):
        for i in range(0, len(ReadFile['flowVersions'][index]['targets'])):
            ReadTarget = ReadFile['flowVersions'][index]["targets"][i]['name']
            print('targetName is : ', ReadTarget)
            targetName_check = ReadTarget.startswith('TGT_')

            if targetName_check:
                print("targetName is vaild and starts with TGT_:", targetName_check)
            else:
                rint("targetName is not starting with TGT_:", operatorCheck)


main()

标签: pythonpython-3.x

解决方案


不要在 python 中编写 BASIC。

重构了部分代码。

看一下os.path.join

您的功能target将无法运行

def main():
    for file_json in os.listdir(path_to_json):
        if not file_json.endswith('.json'): continue
        print("""""######starting######""")
        print(file_json)
        try:
             with open("{}/{}".format(path_to_json,file_json), 'r') as json_file:
                json_data = json.load(json_file)
                print("JSON File is Valid")
                flowVersions = json_data['flow']['flowVersions']
                get_status(flowVersions)
                source(flowVersions)
                operators(flowVersions)
                target(flowVersions)

        except Exception as e  :
            print (e)
            print("JSON File is Invalid")

def get_status(flowVersions):
    for flow in flowVersions:
        print("status is : ", flow["status"])

def source(flowVersions):
    for flow in flowVersions:
        for topic in flow['topics']:
            name = topic['name']
            print('sourceName is : ', name)
            sourceName_check = name.startswith('ES_')

            if sourceName_check:
                print("sourceName is vaild and starts with ES_:")
            else:
                print("sourceName is not starting with ES_:")

推荐阅读