如何让python文件运行另一个不在同一目录中的文件?

时间:2017-07-16 16:46:39

标签: r python-2.7

这是我的代码...我在stackoverflow上尝试了很多解决方案,但是我无法运行另一个不在同一目录下的python文件

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"

输出:

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"

当我运行run.py时,我得到了所需的结果。

输出:

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

但是当我通过f.py

运行时出现以下错误

错误:

  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 个答案:

答案 0 :(得分:1)

r放在文件路径前面,这样反斜杠\就不会转义您不希望转义它们的字符串(see also this question)。< / p>

其次os.system('python file')将逐字执行python file,您的变量未被使用。

还要确保您的文件路径没有换行符(只有一个长行)。

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"