I'm developing a client
that le .JSON
files and sends them to an API. The files are periodically deposited into a folder. However, what I want is that when an error occurs with the file (file being used, file with syntax error, etc.), that file is moved to another folder and client
ignore this file and keep reading the others. How can I do it?
Follow me code so far:
for file in glob.glob(diretorioIn +'*.json'):
print("Iniciando Leitura")
try:
payload = json.load(open(file))
print(file)
print(payload)
print(file)
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
url = 'http://10.24.1.71/track/add'
result = requests.post(url, data=json.dumps(payload), headers=headers)
print(result.text)
os.remove(file)
except ValueError:
print("Arquivo com erro: ")
print(file)
os.rename(file, diretorioErro)
except PermissionError:
print("Arquivo está sendo usado. Será movido para a pasta ERROR")
print(file)
os.rename(file, diretorioErro)
print("Nenhum arquivo encontrado. Reprocessando WebService")