0

How can I write a function that makes an increment name with the incoming files?

def callback(ch,method, properties, body):

    Payload = body.decode("utf-8")
    Payload = ast.literal_eval(Payload)

    with open("received.png", "wb") as f:
        f.write(Payload)

    print(type(Payload))
    print("Data Received : {}".format(Payload))

Here I receive an image.png and name it received.png. I want to write a function that automatically renames the next incoming image something like received_2.png

emin76
  • 1
  • 1

1 Answers1

0

You can get number of files in the current directory and add 1 to it. Here is code to get number of files in current dir

import os, os.path

print(len([name for name in os.listdir('.') if os.path.isfile(name)]))
Franciszek Job
  • 388
  • 2
  • 9