Stack Exchange
How to execute a python code in android java ? (linking a python code and android studio java !!)
I want to link the android java with python (k nearest neighbors) 
This is the java code I used to link python
java code
try {
        Process p = Runtime.getRuntime().exec(
                "python C:/Users/ASUS/Desktop/sendingCode/testttt/app/knn.py");
        Scanner in = new Scanner(new InputStreamReader(
                p.getInputStream()));
        String line = "";
        while ((line = in.nextLine()) != null) {
            // display each output line form python script
            System.out.println(line);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

and this is the python code (knn.py)
import csv
import math
import operator
def loadDataset(filename, trainingSet=[]):
with open(filename, 'r') as csvfile:
    lines = csv.reader(csvfile)
    dataset = list(lines)
    for x in range(len(dataset)-1):
        for y in range(32):
            dataset[x][y] = float(dataset[x][y])
        trainingSet.append(dataset[x])
        #new_model = SelectFromModel(trainingSet[x], threshold=0.5)
        #print (trainingSet[x])
def euclideanDistance(instance1, instance2, length):
distance = 0
for x in range(length):
    distance += pow((instance1[x] - instance2[x]), 2)
return math.sqrt(distance)
def getNeighbors(trainingSet, testInstance, k):
distances = []
length = len(testInstance)-1
for x in range(len(trainingSet)):
    dist = euclideanDistance(testInstance, trainingSet[x], length)
    distances.append((trainingSet[x], dist))
    #print (distances[x])
distances.sort(key=operator.itemgetter(1))
neighbors = []
for x in range(k):
    neighbors.append(distances[x][0])
    #print (neighbors[x])
return neighbors
def getResponse(neighbors):
classVotes = {}
for x in range(len(neighbors)):
    response = "{0},{1},{2},{3} {4}".format(neighbors[x][0], neighbors[x][1], neighbors[x][2], neighbors[x][3], neighbors[x][4])
    if response in classVotes:
        classVotes[response] += 1
    else:
        classVotes[response] = 1
sortedVotes = sorted(classVotes.items(), key=operator.itemgetter(1), reverse=True)
return sortedVotes[0][0]
def save(name,a):
 f=open(name+'.data','wt')
 try:
     writer = csv.writer(f)
     writer.writerow(a)
 finally:
     f.close()
trainingSet=[]
testItem=[0.47968, 0.41193, 0.36192, 0.28804, 0.42813, 0.21444, 0.36239, 0.16019, -0.2966, 0.38776, -0.37455, 0.34006, 0.02174, 0.53456, -0.39664, 0.24938, 0.3724, -0.58612, -0.11107, -0.2086, 0.39568, 0.47285, -0.28453, -0.02871, -0.01756, 0.4725, -0.29967, -0.44055, 0.44525, -0.12222, -0.08769, -0.52123]
loadDataset('f.csv', trainingSet)
#print ('Train set: ' + repr(len(trainingSet)))
#print ('Test Item: ' + repr(len(testItem)))
# generate predictions
predictions=[]
k = 1
neighbors = getNeighbors(trainingSet, testItem, k)
print (neighbors)
save('neighbors',neighbors)
result = getResponse(neighbors)
predictions.append(result)
save('predicted',repr(result))
print('predicted=' + repr(result) + ', actual=' + repr(testItem))
#print (confusion_matrix(trainingSet, predictions))

It didn't work for me. I don't know why ?
I tried to replace knn.py code with another python code and it worked. 
What is the wrong with my python code?What should i use as another way to link between them correctly?

Please post the error or more info about what is not working.

There is no error.. how can I know??

well, what is not working? what output are you expecting? do you see any error?

Because i add save in file statement in the main function inside the knn.py file save('neighbors',neighbors) so that the i can know if the compiler run it or not in case if the file saved but it didn't work (didn't saved the neighbors file ) ..... while when i runed the same knn.py in python IDLE its worked and seved the neighbors file ... I don't know why it doesn't work in java?

Your code just show that you're able to read a text file (which is incidentally is a python script)

Yea but it doesn't read or run this knn.py file either the print statement nor the save neighbors file... while its works with other puthon code that save file or print a massage

I used the same java code with another python code that print a statement and save a value in file then i runed it and I found that saved file and it's print that statement very well.... the same I used it here in knn.py but it's doesn't worked nor I didn't find those files so I realized that the compiler didn't see them !!
