My objective is to use Python's subprocess module to run the following 'conda list>[fullPath_based_on_date]' command in the Anaconda prompt environment:
r'conda list>z:\backup\Anaconda\conda_list_2024-05-06_13-23-45.txt'
I have not been able to find a way to get subprocess to run that command in an Anaconda environment. But I composed the Python script below hoping that someone will give me some hints about how to fix it, and where I can learn about what I did wrong. Here is my Python script:
import os
import subprocess
from datetime import datetime as DTT
NL, TAB, NLT = '\n', '\t', '\n\t'# {NL}{TAB}{NLT}
DT_now_str = (f'''{(DTT.now()).strftime("%Y-%m-%d_%H-%M")}''')
DT_now_str_wSeconds = (f'''{(DTT.now()).strftime("%Y-%m-%d_%H-%M-%S")}''')
print(f'''{DT_now_str = }{NLT}''' +
f'''{DT_now_str_wSeconds = }''')
dst_fullPath_based_on_date = r'z:\backup\Anaconda' + os.sep + 'conda_list_' + DT_now_str_wSeconds + '.txt'
cmd_conda_list_piped2txt = r'conda list>' + dst_fullPath_based_on_date
print(f'''{NL}{cmd_conda_list_piped2txt = }''')
subprocess.run(cmd_conda_list_piped2txt)
print(f'''{NL}{exitcode = }''' +
f'''{out = }''' +
f'''{err = }''')
cmd_edit_conda_list = r"C:\Program Files\Notepad++\notepad++.exe" + ' ' + dst_fullPath_based_on_date
print(f'''{NL}{cmd_edit_conda_list = }''')
subprocess.run(cmd_edit_conda_list)
print(f'''{NL}{exitcode = }''' +
f'''{out = }''' +
f'''{err = }''')
Any suggestions about how to get the subprocess module to run the 'conda list>[fullPath_based_on_date]' command in an Anaconda environment would be much appreciated.
Thank you, Marc
# runme.py
from datetime import datetime
import subprocess
datetime_str = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
filename = f"{datetime_str}.txt"
with open(filename, "w") as f:
subprocess.run(["conda", "list"], stdout=f)
print(f"{filename} written!")
Execute the script within the PowerShell Prompt
or CMD.exe Prompt
:
python .\runme.py
e.g.
(base) PS C:\Users\HP> python .\runme.py
2024-05-16_16-04-46.txt written!
(base) PS C:\Users\HP>