I just want to launch my program written in C++ from a Python script.
I wrote the following script:
import subprocess
subprocess.call(['l:\Proj\Silium.exe', '--AddWatch c:\fff.txt'])
But to my c++ application the parameter "--AddWatch c:\fff.txt" arrives without hyphens - it arrives as "AddWatch c:\fff.txt". So my program doesn't work.
Why does this happen, and how can I fix it?
UPD: thx for comments - yours answer helps!
I explain the issue and the solution.
I need to launch my application in the following way:
l:\Proj\Silium.exe --AddWatch c:\fff.txt
When I tried to do this using some hint from internet:
import subprocess
subprocess.call(['l:\Proj\Silium.exe', '--AddWatch c:\fff.txt'])
the key "--AddWatch" arrives to my program without hyphens - like "AddWatch".
The solution is quite simple:
import subprocess
subprocess.call(['l:\Proj\Silium.exe', '--AddCMakeWatch', 'c:\fff.txt',])
And issue gone away.
P.S.: its very strange that my initial code didnt work, I dont have any idea why python corrupt the command line, I think it is the python bug.
Related
I am well aware that there are multiple threads dealing with this problem because I used them in an attempt to self-teach me how to do it. However, for me it does not work and I wondered if someone could help me find my error.
So I got one program (let's call it prog1) that calls a script for every string-variable like this:
os.system(r'C:\Users\user\docs\bla\script.py %s'%variable)
In script.py I now just wanted to test if this call worked by disabling all code but just doing:
def main(string):
print(string)
root
if __name__ == "__main__":
print('I got executed')
main(sys.argv[1])
The Problem is, if I execute prog1 nothing happens. No errer - so it runs through and the console pops up for a Brief second. However, nothing happens - no string is printed. If I execute script.py directly it works though.
EDIT: I got it to execute without error with the subprocess package by using:
subprocess.call(r'C:\...\script.py' %s' %variable,shell = True)
However, the problem stays the same - nothing happens. Or well, I noticed now that in the Background a Windows window pops up asking me with which program I want to execute files with the ending '.py' but this is not helping me, because I want to execute in console :(
Try using the subprocess module, it allows for more flexible parametres and other configuration changes :
import subprocess
subprocess.check_call(["python.exe", r"C:\Users\user\docs\bla\script.py", variable])
And when executed :
subprocess.check_call(["python", r"C:\Users\user\docs\bla\script.py", "Blue"])
I got executed
Blue
More information about the subprocess : https://docs.python.org/2/library/subprocess.html
I hope the title makes sense. To give specifics:
I am using csvtotable (https://github.com/vividvilla/csvtotable) to generate HTML tables from CSVs. I have installed via pip and am able to run a command line command:
csvtotable test1743.csv test1743.html
to generate a HTML page. All good so far.
I wanted to do this from within a Python script I had already written so I heard that subprocess was the way to do this. I looked up how to do it and understood that it can be done using the following:
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
So I tested this via the command line first by doing
python
from the command line and then running
import subprocess
subprocess.run('csvtotable test1743.csv test1743.html',shell=True)
Success! It worked. Fantastic.
However, when I try to do this from IDLE, it just returns a 1. I have checked the directory thinking that maybe the csv was missing from there, but it still doesn't work.
Am I misunderstanding how subprocess works?
Solved by finding a way to call the function without subprocess. I think the issue may have related to default arguments not being set when it is executed through python and hence why below I have had to specify so many arguments.
Code:
from csvtotable import convert
content = convert.convert("C:\\Users\\admin\\Google Drive\\test1743.csv",delimiter=",",quotechar='"',display_length=-1,overwrite=False,serve=False,pagination=True,virtual_scroll=1000, no_header=False, export=True, export_options=["copy","csv","json","print"])
convert.save("C:\\Users\\admin\\Google Drive\\test1743.html",content)
Note that the argument names had to be changed where they had a - in the name. I just changed any instance e.g. display-length to display_length in convert.py
Whenever I try to display symbolic math in Spyder via the IPython console, several black console windows pop up and then disappear in quick succession. It prints the expression, but I'd like to know if there is a way to get rid of these windows. The windows have the title "C:\Program Files\MikTex 2.9..." if that helps.
It looks like someone already figured it out and posted a solution on GitHub. This is the link: https://github.com/sympy/sympy/issues/11882
It took me (as a novice) some time to figure out exactly what he did, so the following is just a more detailed explanation:
You first need to find the compatibility module in the sympy package. For me, it was located at "C:\Users\Lucas\Anaconda3\Lib\site-packages\sympy\core\compatibility.py". Next, you need to search (in the source code of that module) for the check_output function. The surrounding code should look something like:
# check_output() is new in Python 2.7
import os
try:
try:
from subprocess import check_output
Finally, you need to get rid of the last line, and replace it with the code found in the GitHub link. The resulting block should look like:
# check_output() is new in Python 2.7
import os
try:
try:
from subprocess import check_output as subprocess_check_output
def check_output(*args, **kwargs):
return subprocess_check_output(*args, **kwargs, creationflags=0x08000000) # CREATE_NO_WINDOW
It appears to me that he defines a function which takes the place of check_output, except that the argument to suppress the output windows is always fed in. Hope this helps anyone else having this problem, and I appreciate the fix from Adam on GitHub.
I submitted a pull request to fix this for good:
https://github.com/sympy/sympy/pull/12391
I have a number of different codes which all take a text file with data as input and write to a different one as output. Three of these codes are written in Python 2.7, but one is written in IDL. My goal is to create one "master" python program which can run all of these codes, by typing "python master.py". However, because of the limitations of my system, I am unable to use the 'pyIDL' or 'pyIDLy' modules referred to in this question. Not sure if it matters, but this is using a linux command prompt.
Currently my 'master.py' code looks like this:
import os
os.system("python pycode_1.py")
os.system("idl")
os.system(".com idlcode.pro")
os.system(".r idlcode,"imputfile.dat"")
os.system("exit")
os.system("python pycode_2.py")
os.system("python pycode_3.py")
This code runs the first python code and enters IDL fine. However, it does not enter the later comands into IDL. This means that the IDL command prompt comes up, but I cannot run the IDL code that follows.
I would be very appreciative about any advice to solve this issue.
Thanks in advance!
If you have IDL 8.5 or later, it ships with the IDL-Python bridge built in. Then your code would look something like:
from idlpy import *
IDL.idlcode()
Hope this helps.
So I have worked out a solution that seems to work well for this problem. The issue above was the use of the os.system function to do things it couldn't. My new codde is:
import os
import subprocess
os.system("python python_code1.py")
p=subprocess.Popen("idl", stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(".com idlcode.pro\n")
p.stdin.write("idlcode\n")
p.stdin.write("exit")
p.wait()
os.system("python python_code2.py")
os.system("python python_code3.py")
Encountered a couple of problems with my python program. Basically, I want to be able to send the path files of multiple images to the command prompt (the user defines how many images, so just putting the direct file paths as args, as below, doesn't do what I want). My code currently looks like this:
os.system("java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar image_00000001.jpg image_00000002.jpg image_00000003.jpg")
There are many more images, so of course, writing out image_00000004, 5, 6, etc, is hardly efficient and depends entirely on there being the same number of images each time, which there isn't. (jpivc.jar is the program that opens upon execution of this code, and imports the images and does stuff - that bit works fine). Ideally, the code would be something like:
for i in range(1, NumberOfImages):
os.system("java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar image_0000000" + i + ".jpg")
Except, you know, without opening jpivc.jar each time i is incremented, I'm just using the above to show the kind of thing I want. Unfortunately this doesn't work, as it only sends the first part in " " to the command line, if it doesn't give an error message - my first question is, is there any way of making this do what I want it to do? I'm relatively inexperienced at python, so please be as gentle as possible with the technical details.
My second question is - is there a way of then closing either the command prompt or jpivc.jar? I've tried all the predefined functions I can think of - os.system("tskill cmd.exe") and variatons thereupon, os.kill() (although I'm using 2.5, so I'm not surprised this doesn't work), Popen.kill() and Popen.terminate(), and even tried writing my own kill function using ctypes. Nothing works - until either cmd.exe or jpivc.jar are closed manually, then everything in the rest of my code works perfectly. I think Python halts until the command line is closed - which isn't helpful when I want to then close the command line from Python! To sum up; why does it halt like that, and how can I fix it?
More extracts from my code can be provided if needed - I'm using Python 2.5.4 on Windows XP Professional. Don't ask me to provide anything from jpivc.jar - I won't even pretend to understand the slightest bit of Java.
Thanks in advance,
Dave
I believe you are looking for something like this
fileNames=""
for i in range(1, NumberOfImages):
fileNames += "image_0000000%d.jpg "%i
os.system( "java -jar C:\\Inetpub\\ftproot\\JPivSource\\jpivc.jar %s"%fileNames )
Rename your file from script.py to script.pyw to avoid the opening of command prompt.
jpivc.jar will remain open until you close it manually or programmer changes its code to quit after it completes processing all files.
[EDIT]
I found this for starting and killing a process
pidId = os.spawnl(os.P_NOWAIT, "\\windows\\notepad.exe")
import win32api
win32api.TerminateProcess(pidId ,0)