1

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:

  • Stage filename - staging_userdata_202211-20-4-53.csv
  • Prod filename - production_userdata_202211-23-3-35.csv

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"
  ]
}
4
  • os.path.join will join (dir1, dir2, dir3, ..., file) into a path. You don't want this. Just prepend os.environ['ENV'] to file_suffix with os.environ['ENV'] + file_suffix. Commented Jan 6, 2022 at 21:43
  • Did you mkdir /tmp/staging? You can't create a file in a directory that doesn't exist. Commented Jan 6, 2022 at 21:45
  • And. @erip, I believe that's EXACTLY what he wants. The fact that the path in the error message is correct says that. Commented Jan 6, 2022 at 21:46
  • @TimRoberts I don't think so. See "expected behavior". Presumably OP wants to join some base_dir with the env-based filename. Commented Jan 6, 2022 at 22:07

1 Answer 1

2

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"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.