姜戈 1.9 | views.py |执行*.bat文件 |保存文件的位置

DJANGO 1.9 | views.py | execute *.bat file | location for save file

我有 2 个问题(我在 Whosebug 上找不到答案):

第一题:

我将 run_command.bat 文件添加到:

DjangoProj/
---DjangoApp/
------views.py
------run_command.bat

在方法 save_logsDjangoProj/DjangoApp/views.py 我试过:

def save_logs(request):
    choosenMachines = request.GET.getlist('mvsMachine')
    (data,errors) = subprocess.Popen(r'run_command.bat' + str(choosenMachines), shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE).communicate()

但是我得到了这个错误:

the run_command.bat is not recognize as external or internal command, exec file or batch file

我想 Django 目前在另一条路径上(问题是哪条路径)

第二个问题:

DjangoProj/DjangoApp/views.py

方法创建的txt文件保存在哪里
def set_parameters_on_ftp(request):
    with open('start_task.txt', 'w') as f:
        for command in commands:
            f.write(command+'\n')
    return

假设它应该在:DjangoProj/DjangoApp/*

你猜对了。 Django 当前 运行 路径不在您的项目文件夹中。 在我的测试中,它位于 C:\Python27 您必须提供确切的路径或在设置文件中使用 PROJECT_ROOT 变量。 玩得开心

第一个问题:

import os

#Set myPath variable to the path of the file being executed
myPath =  os.path.dirname(os.path.abspath(__file__))

#Change current working directory to myPath
os.chdir(myPath)
#Or change current working directory to a subdirectory of myPath
os.chdir(os.path.join(myPath, 'subFolder'))

第二个问题:

import os

#Check the current working directory. The txt file is getting saved here.
os.getcwd()

#This can be changed by changing the working directory as described in the answer to the first question.

编辑:更改了第一部分中的 os.chdir() 语法错误。