Checking if a .cmd file was executed successfully with python - python

I wrote a script that executes certain .cmd files. I'm trying to find a way to check if the execution finished with errors or not. This is how the final line of the .cmd file looks like, it shows you the number of warnings and errors:
https://i.stack.imgur.com/y6K1m.png (sorry i do not have enough rep to make the image embedded between the text :()
I tried saving the console output to a variable, and then check if the substring "Error(s)" was inside the text, but that didn't seem to work... I'm fairly new to python and I'm running out of ideas and to stuff to try, any suggestions would be appreciated. Let me know if you need more details. Thanks in advance guys!

Batch scripts usually have a return code that you can check if it completed successfully.
Check this link for the exit codes
And check this question for the python code to get them

Related

How to make visual studio update its error message in a python file?

I wrote several programs in Visual Studio Code, in python.
I always have issue with it:
For instance, I tried to execute a program taking a file as a variable (so a .txt)
I made a mistake on the document name when I called the function ,so ofc, I get an error ( I wrote test1.xt instead of test.txt), so I correct it. However, everytime I execute the program, I still have the test1.txt error, even though I corrected it into test.txt. What should I do ?!
Thanks in advance...
I tried to execute the program again, deleting the call of the function, and doing another call without any success...
Well, I am kind of dumb, but you have to save the file lol
My bad
EDIT: I come from pyzo, so I'm kinda new to VS Code.

Running python code from a text file into your python program

I Want to save my python code as a text file then make a python launcher that once you log in the program starts executing code from the text file. Is This possible to do and if so can someone please help
The request is a bit obscure but if I got it right you want create a python script that is able to launch other python scripts.
I do not understand the use of this but here's a possible solution, keeping in mind that your "text file" was saved as "yourscript.py":
from subprocess import call
call(["python", "yourscript.py"])
This code will act like if you'd run the saved "yourscript.py" from the command line.
Consider using runpy, e.g.:
import runpy
runpy.run_path("yourscript.py", init_globals={'global_name':some_value})
With optional init_globals parameter you can control global objects within the runtime of the script you are calling.

Run python in Mac terminal. Sometimes it runs well, sometimes it gets stuck and no response

I'm self-learning Python by following Coursera python for everybody course. I wrote several files yesterday and all of them confront with a common issue while running in Mac terminal, sometimes it runs fast and well but most of the times it gets stuck and has no response for a long time. For example, one of the files gets stuck and my terminal looks like following:
TopSecrets#StevendeMBP: ~/Desktop/Coursera/Python for everybody/Course3 > python3 CA13_JSON.py
Retrieving http://py4e-data.dr-chuck.net/comments_55310.json
(it got stuck here...)
I asked my friend and was told this issue may result from my terminal buffer limit. I googled some thread and changed the limit, but it doesn't help.
My 6 code and one function package called in code (bs4, for BeautifulSoup) are zipped together. The google drive link is:
enter link description here
You can run any one of them several times in terminal, see if they got stuck.
Thank you very much!
It works perfectly for me. On MacOS, you should perhaps remove or comment the second line (os.system("clear all")) that supposed to clear the terminal screen but doen't change the result. The output is:
Retrieving http://py4e-data.dr-chuck.net/comments_55310.json
User count: 2
total sum is 2351
If it still doen't work, I suspect it hangs fetching http://py4e-data.dr-chuck.net/comments_55310.json with your internet connection that seems unstable.

Code inside of a python file deleted

The code inside of a python file randomly deleted, is there anyway to restore? It is a file of 3.70 KB but when opened and put into a text document there is nothing.
Open with python to see what it contains
with open('deleted.py', 'rb') as f:
print(repr(f.read()))
Since you are a new user I am assuming you are new to code development etc. Therefore, you should look at some versioning control tools like:
SVN
Github
Gitlab
There are some more, but these are the most common ones. They are used to store your code and to revert you code if you mess up. They are also used to merge codes when different programmers are changing it. For the moment this will not help but will help in the future.
For now you may look at some restore tools but I highly doubt it that you are able to recreate the file. Another possiblity is: when you haven IDE to look at your command history. Maybe you executed the script and you can find the executed script as commands in the command history.

Python interactive library(pymidas) session warning

I'm using Python2.7 and a library called pymidas.
Within my python script I call the library with the following comand:
from pymidas import midas
midas.do('INDISK/FITS test.fits test.bdf')
All the code that I have further written does exactly what I want, but whenever the script imports midas I first get a welcome output of (py)midas, which is ok with me, but afterwards it asks me if I want a parallel or a new session.
Saddly this point needs human interaction in selecting parallel mode. By reading the documentation of midas I found, that midas has an option (-P) which causes exactly what I need, and forces midas to open without any questions asked and directly going to parallel mode.
Does anybody know how to achieve this in my python script?
Thanks!
At the end of your script add :
midas.do('.exit')
This ensures you dont get asked the next time you run the script.

Categories