python - open file in application - python-2.7

I am using terminal to sucsessfully open a file in quicktime player 7, but can't seem to get it working using python to do the same thing.
So this is working from terminal:
open -a "Quicktime Player 7" /Users/Me/Movies/test.mov
But this is not working in python 2.7, it opens quicktime, but not the file:
command = ('open -a "Quicktime Player 7"', 'Users/Me/Movies/test.mov')
subprocess.Popen(command, shell=True)
What am I doing wrong?

If you pass command as list/tuple, you have to split the arguments up correctly:
command = ('open', '-a', 'Quicktime Player 7', '/Users/Me/Movies/test.mov')
subprocess.Popen(command, shell=True)
Then I think you should also be able to drop the shell=True parameter. Further, you could look into subprocess.call() or subprocess.check_call() (the former returns the return value of the program, the latter raises an exception if the return value indicates an error):
subprocess.check_call(['open', '-a', 'Quicktime Player 7', '/Users/Me/Movies/test.mov'])
NB: Coding-style-wise, command is usually passed as list, as seen in the docs I linked above.
Edit: Add '/' at the beginning of both paths to make it work.

Related

No stdout when attempting to run a python file with subprocess.run()

I'm trying to run a python file from within my python script. I'm doubtful that the file is even getting run in the first place and it is not showing anything in the stdout either for me to debug.
I have tried the command 'ls' in subprocess and it worked, and was in the proper directory that the temp.py file i am trying to run is.
When i have tried to set the argument 'shell=True' it takes me into the python repl for some reason i am not sure why.
Here is the string output:
Terminal output: CompletedProcess(args=['python3', 'temp.py'], returncode=0, stdout=b'')
And here's the code used to produce it:
result = subprocess.run(['python3', 'temp.py'], stdout=subprocess.PIPE, check=True)
print('Terminal output: '+str(result))
EDIT
I also just tried
process = Popen(['python3', 'temp.py'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
print('Terminal output: '+str(stdout)+str(stderr))
No cigar:
Terminal output: b''b''
So, I found an alternative that worked. I dumped all the contents of the file into the python3 command with the '-c' augment.
process = Popen(['python3','-u', '-c', strCode], stdout=PIPE, stderr=PIPE)
strCode being the contents of the file.

How to call the python program in update mode using pre-buid program?

I have been following the "Making an Emotion-Aware Music Player" tutorial and everything was works fine until running the main program in update.
I searched for "argparse " in tutorials and found nothing about this method.
I have a very little knowledge about python so that could anyone please explain me how run the main program in update mode and where should type the following code.
source page: Making an Emotion-Aware Music Player
Once the file is downloaded, call the main program in “update mode” like this:
~
#To activate update mode, pass the --update flag
python <filename.py> --update
#To get help, use the -h or --help flag
python <filename.py> -h
python <filename.py> --help
When I ran the above code in Command prompt it gave me a error, which says there is no XML file. But as for the main program it should create XML if there wasn't.
I'm using Visual studio 2015 with python 2.7

QProcess can launch programs but not python (command line)

The following code is failing to launch the python command line.
QProcess *myProcess = new QProcess(this);
myProcess->start("\"C:\\Program Files\\Python27\\python.exe\"");
If I replace python27 with (for example)
myProcess->start("\"C:\\Program Files\\Notepad++\\notepad++.exe\"")
notepad opens. Why is my program able to launch notepad but not Python Command Line?
I tried using startDetached() as suggested here but that didn't make a difference.
QProcess::Error() gives me error 5: unknown error.
If you just want to use the 'python console' you must use cmd.exe application from windows
You must have python in PATH so the windows console will know where to take it from.
So, you can try: QProcess::startDetached("cmd", "python")..see more specific syntax details here
It seems I've misunderstood what happens when you launch a command line. I was expecting the python command line or command prompt window to open.
It turns out that if I just pass my commands as arguments start() like so:
myProcess->start("cmd.exe /C python C:\\Users\\SP4\\Desktop\\helloworld.py");
Command prompt runs my python script and I get the output ("Hello World") using:
QString output = myProcess->readAllStandardOutput();
All this happens in the background and you can't actually see a command line window open and print out "Hello, World".
Please correct me if I've misunderstood something.

Run python script with droneapi without terminal

I managed to run examples in command prompt after running mavproxy.py and loading droneapi. But when I double click on on my script, it throws me "'local_connect' is not defined", it runs in terminal as was told above, but I cannot run it only with double click. So my question is: Is there any way to run script using droneapi only with double click?
using Windows 8.1
Thanks in advance
You'll want to look at the Running an App/Example section of the guide. For now, you can only run a DroneKit script by launching it from inside the MAVProxy terminal. For example, after launching:
$ mavproxy.py --master=127.0.0.1:14550
MANUAL> module load droneapi.module.api
DroneAPI loaded
You can use the api start command to run a local script:
MANUAL> api start vehicle_state.py
STABILIZE>
Get all vehicle attribute values:
Location: Attitude: Attitude:pitch=-0.00405988190323,yaw=-0.0973932668567,roll=-0.00393210304901
Velocity: [0.06, -0.07, 0.0]
GPS: GPSInfo:fix=3,num_sat=10
groundspeed: 0.0
airspeed: 0.0
mount_status: [None, None, None]
Mode: STABILIZE
Armed: False
I think Sony Nguyen is asking for running the vehicle_state.py outside the Mavproxy command prompt, just like runnning the .py file normally.
I'm also looking for a solution as well.
You can only run dronekit from mavproxy at the moment (its structured as a mavproxy module, there are plans to restructure it), however if you simply want to avoid having to load up MavProxy and then running code manually, you can use the cmd flag:
mavproxy.py --cmd="api start app.py"

command prompt appears than immediately disappears

I encountered this problem after following a python tutorial on Youtube about creating text files. The instructor had us type in the following code to start:
def createFile(dest):
print dest
if__name__ == '__main__':
createFile('ham')
raw_input('done!')
We had created a folder on the desktop to store the file 'ham' in. But when I double clicked on 'ham' the command prompt window popped on then in a flash it popped off. I am an obvious beginner and I don't know what is going on. Can anyone explain this to me?
You can open command prompt then navigate to python interpreter
directory and run your program by typing python /diretory/to/your/program.py for
example if you have a program named test.py in the directory c:/python and you want to
run it and you have python interpreter installed in C:/python2.x/ directory
then you should open command prompt and type in
cd c:\python2.x\
then you should type
python c:/python/test.py
and perfectly it will work
showing you your program output .