codGmer
Posts: 18
Joined: Wed Feb 07, 2018 7:48 am

Executing a file

Tue Feb 13, 2018 9:41 pm

Hello,

I want to be able to execute a file called 'sendmidi' in a python script with parameters. This is the normal command:

Code: Select all

sendmidi dev 0 pb (variable)
where variable needs to get a variable ofcourse. The Path of the file is /home/pi/SendMIDI

I have tried using subprocess.Popen() but i always get errors doing that.

The variable is called 'pot1'

jahboater
Posts: 3280
Joined: Wed Feb 04, 2015 6:38 pm

Re: Executing a file

Wed Feb 14, 2018 7:44 am

Try os.system() instead of popen().

You can just use "+" to concatenate strings.

pcmanbob
Posts: 4549
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Executing a file

Wed Feb 14, 2018 12:14 pm

How did you do the call in subprocess.Popen() ?

This is an example of using subprocess.Popen() to call nmap I have added lots of comments explaining each line it might help you solve your problem.

Code: Select all

#!/usr/bin/python
## get subprocess module 
import subprocess
 
## call Nmap command ##
p = subprocess.Popen("sudo nmap -sP -PI -PT 192.168.1.1/24 | grep -i hosts", stdout=subprocess.PIPE, shell=True)
 
## Talk with Nmap command i.e. read data from stdout and stderr. Store this info in tuple ##
##Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.##
(output, err) = p.communicate()
 
## Wait for nmap to terminate. Get return returncode ##

## This line gets the command exit status and saves it as status this will be 0 if all is well ##
status = p.wait()

## This line saves the output of the namp command as resultline ##
resultline = output
Note the actual line to call nmap is enclosed with " "
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

codGmer
Posts: 18
Joined: Wed Feb 07, 2018 7:48 am

Re: Executing a file

Wed Feb 14, 2018 7:19 pm

Thanks ppl, i got it working but a lot of the times when i run the code its running slowly.

Im sending simple midi signals to the pc.

Code: Select all

#!/usr/bin/python
# -*- coding: utf-8 -*- Simple example of reading the MCP3008 analog 
# input channels and printing them all out. Author: Tony DiCola License: 
# Public Domain
import time
import os
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

SPI_PORT = 0
SPI_DEVICE = 0
mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
os.chdir("/home/pi/SendMIDI/")
while True:
 pot0 = str(mcp.read_adc(0) * 16)
 time.sleep(0.0001)
 #if pot0c != pot0:
 os.system("./sendmidi dev f_midi pb " + pot0)
So i want to read the MCP3008 A/D Converter and pass it trough the file 'sendmidi'.

I want to be able to control a knob in a software called ableton.

Here's the slow running code:
https://i.gyazo.com/e1a9d70cf433668794a ... 4c7973.mp4

here's the fast running code:
https://i.gyazo.com/2599f4e6ca9b146b55d ... 937a64.mp4

I think it has something to do with the infinite loop not properly executing. Can you guys help to make it run smooth every time?

Return to “Python”