1

This is my code...I have tried many solutions given on stackoverflow but i am not able to run another python file which is not in same directory

f.py

import os   
print "Start..."
file='"C:\Users\Mohit\Desktop\ML PROJECT\Practical Session on R\Practical 
Session on R\Session II - Regression\run.py"'
os.system('python file ')
print "Done"

Output:

Fatal error: cannot open file 'file': No such file or directory

run.py

import os   
print "Start..."
files=r'"C:\Users\Mohit\Desktop\ML PROJECT\Practical Session on R\Practical 
Session on R\Session II - Regression\decisionTree.R"'
os.system('Rscript '+files)
print "Done"

I get the desired result when i run run.py.

Output:

C:\Users\Mohit\Desktop\ML PROJECT\Practical Session on R\Practical Session 
on R\
Session II - Regression>python r.py
Start...

START
elapsed
   0.17

Step 1: Library Inclusion
Step 2: Variable Declaration[1]
[1] "regressionDataSet.csv"

Step 3: Data Loading       RMSD
irNumber
14395 1.167  5364.27   4616.64
15435 2.500 25468.10 475274.00 -
8739  4.039  5921.03   7071.89
11586 0.000 14192.70  75413.00
13765 0.000 17432.40 245147.00 -
10087 3.814  7333.51  26580.70
[1] 16382
[1] "RMSD"          "Area"
[5] "SS"            "ResidueLeng

Step 4: Counting dataset[1] 1638

Step 5: Choose Target Variable[1

Step 6: Choose Inputs Variable[1
"SS"
[5] "ResidueLength" "PairNumber"
[1] 6

Step 7: Select training dataset
th PairNumber  RMSD
14395  5364.27   4616.64   -696.
15435 25468.10 475274.00 -10600.
8739   5921.03   7071.89  -1658.
11586 14192.70  75413.00  -7939.
13765 17432.40 245147.00 -10885.
10087  7333.51  26580.70  -2746.
[1] 8191

Step 8: Select testing dataset
PairNumber  RMSD
10216 20510.40 254178.0 44500.0
2385   9981.42  28499.0 -4789.0
1886  21107.10 192443.0 -4860.0
13684 17765.00  76543.0 -8164.0
10319  6308.91  10287.3 -3370.0
13088  5844.34  11139.7 -1879.7
[1] 8192

Step 9: Model Building ->  decis
ength + PairNumber
n= 8191

node), split, n, deviance, yval
  * denotes terminal node

1) root 8191 23819.88000 2.364
2) Energy< -6161.885 1592  4
  4) ResidueLength< 387.5 74
    8) Energy< -7785.3 262
    9) Energy>=-7785.3 481
  5) ResidueLength>=387.5 84
   10) Energy< -12910.5 173
   11) Energy>=-12910.5 676
     22) ResidueLength< 467.
       44) ResidueLength>=39
       45) ResidueLength< 39
     23) ResidueLength>=467.
3) Energy>=-6161.885 6599 17
  6) SS< 21.5 1584  3795.813
  7) SS>=21.5 5015 13126.850
   14) Energy< -3971.125 163
     28) ResidueLength< 192.
     29) ResidueLength>=192.
       58) ResidueLength< 26
        116) Energy< -4680.5
        117) Energy>=-4680.5
       59) ResidueLength>=26
   15) Energy>=-3971.125 337
     30) Area< 8601.98 1655
       60) Energy< -2933.36
       61) Energy>=-2933.36
     31) Area>=8601.98 1721

Step 10: Prediction using ->  de
13684      10319      13088
3.24618361 0.51722167 3.10727318

Step 11: Extracting Actual[1] 3.

Step 12: Model Evaluation[1] 0.5
[1] 0.25
[1] 1.19
[1] 48.19
elapsed
0.53
null device
      1
       modelName   r    R rm
 elapsed decisionTree 0.5 0.25 1.

  Step 13: Writing to file
  Step 14: Saving the Model -> dec
   Done
 Total Time Taken:  0.53  secDone

But i get the following error when i run it through f.py

Error:

  Start...
  Start...

   START
  elapsed
  0.18

  Step 1: Library Inclusion
  Step 2: Variable Declaration[1] "decisionTree"
  [1] "regressionDataSet.csv"

  Step 3: Data LoadingError in file(file, "rt") : cannot open the connec
  Calls: read.csv -> read.table -> file
  In addition: Warning message:
  In file(file, "rt") :
     cannot open file 'regressionDataSet.csv': No such file or directory

1 Answers1

1

Put an r in front of your file path, so that the backslashes \ don't escape strings where you don't want them to be escaped (see also this question).

Secondly os.system('python file') will literally execute python file, your variable isn't being used.

Also make sure there's no linebreak your file path (just one long line).

import os   
print "Start..."

folder = r'C:\Users\Mohit\Desktop\ML PROJECT\Practical Session on R\Practical Session on R\Session II - Regression'
file= r'"{}\run.py"'.format(folder)

os.chdir(folder)
os.system('python '+file)

print "Done"
chrki
  • 6,143
  • 6
  • 35
  • 55
  • My original problem is solved...but i have edited the question and stuck in another problem.(Description is provided in the body) – Mohit Khandelwal Jul 16 '17 at 18:03
  • @MohitKhandelwal use `os.chdir` to change into the python file's directory (see question https://stackoverflow.com/questions/1810743/how-to-set-the-current-working-directory-in-python). I'm assuming the missing file regressionDataSet.csv and your run.py are in the same folder. I edited my answer. – chrki Jul 16 '17 at 18:30
  • please check my this question – Mohit Khandelwal Jul 24 '17 at 15:05