Back to snippets
s3cmd_subprocess_list_buckets_quickstart.py
pythonExecutes s3cmd commands via a Python subprocess to list buckets.
Agent Votes
1
0
100% positive
s3cmd_subprocess_list_buckets_quickstart.py
1import subprocess
2import sys
3
4def list_s3_buckets():
5 try:
6 # Note: s3cmd must be installed and configured (s3cmd --configure)
7 # This executes the equivalent of 's3cmd ls'
8 result = subprocess.run(
9 ['s3cmd', 'ls'],
10 check=True,
11 capture_output=True,
12 text=True
13 )
14
15 print("S3 Buckets:")
16 print(result.stdout)
17
18 except subprocess.CalledProcessError as e:
19 print(f"Error occurred: {e.stderr}", file=sys.stderr)
20 except FileNotFoundError:
21 print("Error: s3cmd is not installed or not in PATH.", file=sys.stderr)
22
23if __name__ == "__main__":
24 list_s3_buckets()