Search code examples
pythonamazon-web-servicesaws-lambdafilesystemstmp

how to list the files in your /tmp folder


I am using an AWS lambda function to download a file to my /tmp folder.

        try:
            s3.download_file(source_bucket_name, i, f"/tmp/{original_zip_file_name}")
        except Exception as e:
            print('Unable to download S3 file', e)
            sys.exit(1)

The behavior is a bit inconsistent. How can I use Python to now list down the files present in my tmp folder after this?

Edit:

I mean, how can I list down the contents of tmp immediately after this code snippet, within the same lambda function (not after the lambda function ends).

because later I am reading those files from tmp again


Solution

  • As the name suggests (tmp) that folder is not persistent. Lambda works in distributed architecture and the data/files stored inside the /tmp folder may be destroyed once the lambda function execution ends.

    Lambda doesn't guarantee that a function invocation will be reused, so the contents of /tmp (along with the memory of any running processes) could disappear at any time.

    That is the reason you are getting inconsistent result, try other file system like EFS or S3.

    If you want to check whether a file exists inside the /tmp folder you can use

    import os
    os.path.isfile('/tmp/yourfilename')
    
    #Get list of files
    lst = os.listdir("/tmp")