👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
Using boto3, I can access my AWS S3 bucket:
s3 = boto3.resource("s3")
bucket = s3.Bucket("my-bucket-name")
Now, the bucket contains folder first-level
, which itself contains several sub-folders named with a timestamp, for instance 1456753904534
.
I need to know the name of these sub-folders for another job I"m doing and I wonder whether I could have boto3 retrieve those for me.
So I tried:
objs = bucket.meta.client.list_objects(Bucket="my-bucket-name")
which gives a dictionary, whose key "Contents" gives me all the third-level files instead of the second-level timestamp directories, in fact I get a list containing things as
{u"ETag": ""etag"", u"Key": first-level/1456753904534/part-00014", u"LastModified": datetime.datetime(2016, 2, 29, 13, 52, 24, tzinfo=tzutc()),
u"Owner": {u"DisplayName": "owner", u"ID": "id"},
u"Size": size, u"StorageClass": "storageclass"}
you can see that the specific files, in this case part-00014
are retrieved, while I"d like to get the name of the directory alone.
In principle I could strip out the directory name from all the paths but it"s ugly and expensive to retrieve everything at third level to get the second level!
I also tried something reported here:
for o in bucket.objects.filter(Delimiter="/"):
print(o.key)
but I do not get the folders at the desired level.
Is there a way to solve this?
👻 Read also: what is the best laptop for engineering students?
Retrieving subfolders names in S3 bucket from boto3 __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
Retrieving subfolders names in S3 bucket from boto3 __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from Retrieving subfolders names in S3 bucket from boto3, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Retrieving subfolders names in S3 bucket from boto3
- Deutsch Retrieving subfolders names in S3 bucket from boto3
- Français Retrieving subfolders names in S3 bucket from boto3
- Español Retrieving subfolders names in S3 bucket from boto3
- Türk Retrieving subfolders names in S3 bucket from boto3
- Русский Retrieving subfolders names in S3 bucket from boto3
- Português Retrieving subfolders names in S3 bucket from boto3
- Polski Retrieving subfolders names in S3 bucket from boto3
- Nederlandse Retrieving subfolders names in S3 bucket from boto3
- 中文 Retrieving subfolders names in S3 bucket from boto3
- 한국어 Retrieving subfolders names in S3 bucket from boto3
- 日本語 Retrieving subfolders names in S3 bucket from boto3
- हिन्दी Retrieving subfolders names in S3 bucket from boto3
Moscow | 2023-03-26
I was preparing for my coding interview, thanks for clarifying this - Retrieving subfolders names in S3 bucket from boto3 in Python is not the simplest one. Will get back tomorrow with feedback
Moscow | 2023-03-26
Maybe there are another answers? What Retrieving subfolders names in S3 bucket from boto3 exactly means?. I am just not quite sure it is the best method
Abu Dhabi | 2023-03-26
Simply put and clear. Thank you for sharing. Retrieving subfolders names in S3 bucket from boto3 and other issues with StackOverflow was always my weak point 😁. I am just not quite sure it is the best method