0

I am trying to translate this from Bash to Python:

export file="${directory}/scrutation_$(date "+%Y%m%d_%H%M%S").log"

I know that export sets an environment variable, and that (date "+%Y%m%d_%H%M%S") is strftime("%d/%m/%Y, %H:%M:%S") in Python.

This is what I have tried:

import os
os.environ[file]= f"{directory}/scrutation[strftime("%d/%m/%Y, %H:%M:%S")].log"

Is this correct?

Cow
  • 2,543
  • 4
  • 13
  • 25
answer007
  • 5
  • 2

1 Answers1

1

The name of the environment variable is a string, it needs to be quoted.

Double quotes aren't nested in f"", use single quotes for one of the pairs.

$(...) is the Command Substitution, i.e. you need to run the strftime, not include it in square brackets.

Also, you can use the same format string without changes, unless you really want to change the timestamp format.

os.environ['file'] = f'{directory}/scrutation_' \
    f'{datetime.now().strftime("%Y%m%d_%H%M%S")}.log'
choroba
  • 231,213
  • 25
  • 204
  • 289