The RecursionError: maximum recursion depth exceeded
usually occurs when a function calls itself too many times, causing the stack to overflow. The solution is to check the code for infinite loops or unintended recursion, and adjust the recursion limit if necessary. Let’s see an example and solution for how to fix it.
Prerequisites
- python
- sudo privileges
Solution
- Let’s say you are using the AWS SDK for Python (Boto3) to recursively list all objects in an S3 bucket. Here’s some sample code:
import boto3
def list_objects(bucket_name, prefix=''):
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
for obj in bucket.objects.filter(Prefix=prefix):
print(obj.key)
if obj.key.endswith('/'):
list_objects(bucket_name, prefix=obj.key)
- If the bucket has a deep directory structure, this function may eventually cause a
RecursionError
due to excessive recursion. To solve this error, we need to add a base case that stops the recursion. In this case, we want to stop the recursion when there are no more objects to list. Here’s the updated code:
import boto3
def list_objects(bucket_name, prefix=''):
s3 = boto3.resource('s3')
bucket = s3.Bucket(bucket_name)
objects = bucket.objects.filter(Prefix=prefix)
for obj in objects:
print(obj.key)
if obj.key.endswith('/'):
list_objects(bucket_name, prefix=obj.key)
if len(list(objects)) == 0:
return
- In this code, I added a check to see if there are any objects left to list after the loop is finished. If there are no objects left, I return from the function, stopping the recursion preventing the
RecursionError
from occurring.
Conclusion
Feel free to leave a comment below and if you find this tutorial useful, follow our official channel on Telegram.