批处理文件未执行python脚本(来自laravel)

时间:2019-04-29 19:51:02

标签: python-3.x laravel pandas

我正在尝试在Laravel中运行一个批处理文件,该批处理文件将执行python脚本。事实是,当python脚本执行一个简单的任务时,它在一开始就起作用:从数据库中获取CSV文件的文件名,然后将其保存在文本文件中。生成的文件位于公用文件夹中。但是,当我更改python脚本以执行ACP脚本时,执行批处理文件时什么也没发生。

以下是我实际上想做什么的一些解释: python脚本从数据库中获取文件名,然后启动ACP进程。 批处理文件,Python脚本和CSV文件位于同一目录中。另外,请记住,python和批处理脚本在正常执行时都可以正常运行(不是从Laravel执行)。

批处理文件代码[runscript.bat]

<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
  <div class="container">
   <a class="navbar-brand" href="travelindex.html">
    <img src="travel.png" width="30" height="30" class="d-inline-block align-top" alt="">
  </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
    <div class="collapse navbar-collapse" id="navbarResponsive">
      <ul class="navbar-nav ml-auto">
        <li class="nav-item active">
          <a class="nav-link" href="travelindex.html">Home
                <span class="sr-only">(current)</span>
              </a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="reservations.html">Reservations</a>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="aboutus.html">About Us</a>
        </li>
      </ul>
    </div>
  </div>

Python代码:[PythonScript.py]

@echo off
"python path\python.exe" "script path\pythonScript.py"
PAUSE

用于执行批处理文件的Laravel控制器功能

#!/usr/bin/env python
import sys
import mysql.connector
import numpy as np
import pandas as pd

#Connect to database
db=mysql.connector.connect(host="localhost",user="root",passwd="",db="databaseName")

#get the filename
cur = db.cursor()
cur.execute("select nom_fichier from posts where used = 'on'")
i = cur.fetchall()
k = str(i)
thefilename = k[3:-4]

#read the file
thefile=pd.read_csv(thefilename)

#Label Encoder
from sklearn.preprocessing import LabelEncoder
lb_make = LabelEncoder()
for i in thefile.head(0):
    if (thefile[i].dtypes != 'int64') | (thefile[i].dtypes != 'float64'):
        thefile[i] = lb_make.fit_transform(thefile[i])

for i in thefile.head(0):
    if (thefile[i].dtypes != 'float64'):
        thefile[i] = thefile[i].astype(float)

y_file = pd.DataFrame()
y_file = pd.DataFrame(columns=['target'])

y_file['target'] = thefile['Churn']
thefile = thefile.drop(columns='Churn')

#Fit Data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(thefile)

scaled_data = scaler.transform(thefile)

#PCA
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
pca.fit(scaled_data)
x_pca = pca.transform(scaled_data)

#Save result is csv file
np.savetxt("mydataACP.csv", x_pca, delimiter=",")

cur.close()
db.close()

查看脚本

    public function LabelEncoder(){
        exec('path\runScript.bat');
    }

因此,我期望的是公用目录中的“ mydataACP.csv”文件。但是,执行批处理文件时没有任何反应。

0 个答案:

没有答案