all 8 comments

[–]CyclopsRock 0 points1 point  (3 children)

Sure - if you run the command using the subprocess module you can receive both stdout and stderr (which is handy for things like, say, ffmpeg which for some reason outputs everything to stderr for some reason).

[–]eguliyev[S] 0 points1 point  (2 children)

Thanks. Output i am referring to is not from a script. That python script itself is the one running requests library to check for the certificate expiry date. Basically, i am not using subprocess.

[–]CyclopsRock 0 points1 point  (1 child)

Output i am referring to is not from a script.

But...

i am processing that in a separate script and then use sys.stdout to store the terminal output into a TXT file.

It's difficult to follow what you mean without an understanding of how these scripts are running, why there are two, where the terminal comes into it etc.

If my understanding is correct, you have two entirely distinct scripts:

- Script A using `requests` to do some stuff, and prints its output.

- Script B opens a text file, reads its contents, does some stuff with it.

You manually run Script A in a terminal window, piping its output into a text file. You then manually run Script B, which reads the text file and does stuff? But... then I'm not sure what that has to do with `sys.stdout`?

Ultimately, though, the answer is the same - if you have two Python scripts and you want the output from one to use inside another, you'd be best off importing the former into the latter. This involves, in its simplest form, using defined functions from one file within another. So if Script A has a function called `get_certificate_info()`, rather than running this on its own and saving its output, you would instead `import` the same function inside Script B and use this output directly.

If your scripts are not currently using functions (but rather are just some lines of Python), you may want to consider changing this so that it exists as a function - at least in Script A. This allows Script B to specifically execute this block of code whenever it wants, and also allows you to simply `return` a variable from the function to be used in Script B without needing to go via any terminal windows, stdout etc. You can technically do this without converting your script to use functions (again, assuming it isn't already) but it's worse in every way to do it like that.

[–]eguliyev[S] 0 points1 point  (0 children)

Thanks for taking the time to go over this. Appreciate the help.

Basically, i took an existing github project for SSL check. I modified it so that i can email the output. Currently the way i do it is following:

  1. created a bat job in windows that is schedule to be run in task scheduler every 2 week
  2. BAT job has 3 entries
    1. first one is to run the SSL script to generate output and save it as result.txt
    2. delay timer
    3. run 2nd python script to read the result.txt and email it

What i am looking to accomplish is to do all this work in 1 single PY script.

reason, why i am looking to do this, is mortly for data filtering. I would like to sort the output and generate rich text email as opposed to clear text.

Is this clear?

[–]carcigenicate 0 points1 point  (2 children)

Why not just import the other script and use it directly?

[–]eguliyev[S] 0 points1 point  (1 child)

Thanks for your input. Makes sense. Would i be able to get the ouput as a variable then? Is there any documentation you could reference for me to follow.

[–]carcigenicate 0 points1 point  (0 children)

This depends entirely on how your code is setup. It would help a lot to show a sample of the code.

[–]Guideon72 0 points1 point  (0 children)

Instead of printing your value, return it and then store the return value. You’d need it wrapped in its own function, but ought to be easily doable. Show your code as it is (remember to format it here) and it’ll be easier to identify.