Python 在本地运行和在服务器上运行时,程序的当前路径不同

Python 在本地运行和在服务器上运行时,程序的当前路径不同,python,django,apache,Python,Django,Apache,我有一个django web,它在某些操作上使用以下命令调用服务器上的.exe: proc = subprocess.Popen([r'C:/.../django/project/app/program.exe', 'arg1', 'arg2', ..], stdout=subprocess.PIPE) s = proc.stdout.read() proc.wait() 问题是program.exe必须依次打开服务器上的一些文件。这些文件由代码(在c++中)使用相对路径调用,并存储在.exe

我有一个django web,它在某些操作上使用以下命令调用服务器上的.exe:

proc = subprocess.Popen([r'C:/.../django/project/app/program.exe', 'arg1', 'arg2', ..], stdout=subprocess.PIPE)
s = proc.stdout.read()
proc.wait()
问题是program.exe必须依次打开服务器上的一些文件。这些文件由代码(在c++中)使用相对路径调用,并存储在.exe所在的同一文件夹中。当我从IDE或终端运行program.exe时,它就像一个符咒一样工作,但当它在服务器上时就不行了

我用c++编写了一个简单的程序,并将其放置在与program.exe相同的文件夹中,以了解在服务器上运行program.exe时的当前路径:

#define GetCurrentDir _getcwd
int main(int argc, char* argv[]{
    char cCurrentPath[FILENAME_MAX];
    GetCurrentDir(cCurrentPath, sizeof(cCurrentPath));
    std::cout << cCurrentPath << std::endl;
}
#定义GetCurrentDir\u getcwd
int main(int argc,char*argv[]{
char cCurrentPath[FILENAME_MAX];
GetCurrentDir(cCurrentPath,sizeof(cCurrentPath));

std::你到底在问什么还不清楚。这是你问题的答案吗?在子流程打开呼叫帮助之前检查是否执行os.chdir('path-to-your-required-dir')。这正是我需要的。谢谢@Omie!!