Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trouble understanding Bash and translating to Python

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Is this correct?

>Solution :

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading