Ignore file with Python

Asked

Viewed 112 times

1

I’m developing a client that le files .JSON and sends them to an API. Files are deposited periodically in 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 the client ignore this file and continue reading the others. How can I do?

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")

1 answer

0

What you can do is mark those files that are being used or with errors, because from the moment they are in use you will not be able to move them. Place their path in a list/Dict object and remove them after or at a time that ensures they are released.

Tip, read the file using the declaration with:

with open(file_path) as df:
   data = json.load(df)

So you avoid leaving it open.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.