将文件重定向到另一个位置,以防PYTHON中的条件为TRUE



我有一个python代码,它读取嵌套的JSON文件,并根据下面代码中函数(如源、运算符和目标(的条件返回TRUE或FALSE-我可以将其

现在,我想要函数(源、运算符和目标(的一个条件,如果它满足if条件>gt;然后,它应该重定向通过所有验证的文件名(SOURCE、OPERATOR、TARGET(,并将文件名移动到上的另一个位置

例如

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)

输出:如果条件为TRUE——则>gt;打印文件名并>gt>存储在另一个目录中

我正在读取所有的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()

不要在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_:")

最新更新