Reputation: 481
I'm creating a Lambda function with python for STAGE and PROD environment which will generate a random CSV files. But I need to have a dynamic filename.
I have the current code as below:
filename = "/tmp/userdata_%d%d%d-%d-%d-%d.csv" % (ts.tm_year, ts.tm_mon, ts.tm_mon, ts.tm_hour, ts.tm_min, ts.tm_sec)
I have configured ENVIRONMENT VARIABLES within the lambda function (like ENV = prod)
Expected Behavior:
I tried something like below, but ended up with errors as I'm very new to Python.
file_prefix = "/tmp/"
file_suffix = "userdata_%d%d%d-%d-%d-%d.csv" % (ts.tm_year, ts.tm_mon, ts.tm_mon, ts.tm_hour, ts.tm_min, ts.tm_sec)
filename = (os.path.join(file_prefix, os.getenv('ENV'), file_suffix))
Got the below error:
{
"errorMessage": "[Errno 2] No such file or directory: '/tmp/staging/userdata_202211-21-27-36.csv'",
"errorType": "FileNotFoundError",
"stackTrace": [
" File \"/var/task/generate_csv.py\", line 26, in handler\n with open(filename, 'w', newline='') as csvfile:\n"
]
}
Upvotes: 1
Views: 2645
Reputation: 104712
You can easily merge the environment name into the existing string formatting operation you're doing. Just add a %s
where you want the environment name to show up, and put the variable you've stored it in in the right place in the tuple of values to be substituted:
env = os.getenv("ENV")
filename = "/tmp/%s_userdata_%d%d-%d-%d-%d.csv" % (env, ts.tm_year, ts.tm_mon, ts.tm_hour, ts.tm_min, ts.tm_sec)
Note, I removed a duplicated inclusion of ts.tm_mon
twice in the output. It may be that you want the month to always use two digits, in which case you should replace the second %d
with %02d
which will zero-pad one digit month numbers. You may want that for the time numbers too!
It's not necessary, but you could take this as an opportunity to change up your string formatting style. You're using one of the oldest styles, using the %
operator. That style of formatting is based on C's printf
syntax. A newer method is str.format
, and the newest is f-strings. Here's what an f-string version would look like:
filename = f"/tmp/{env}_userdata_{ts.tm_year}{ts.tm_mon:02d}-{ts.tmhour:02d}-{ts.tm_min:02d}-{ts.tm_sec:02d}.csv"
Upvotes: 2