關於工作路徑與模組檔案路徑

2021-05-26 23:28:08 字數 633 閱讀 6045

今天在群裡遇到的問題 。

我在d:\路徑下訪問d:\test\a.exe

a.exe呼叫了它所在路徑下的1.txt

而在d:\下直接訪問a.exe則提示找不到檔案

涉及到了工作路徑 和 模組檔案路徑的問題。

ifstream fin;

fin.open("1.txt");

預設情況下,open呼叫的是當前工作路徑的檔案,即呼叫你所在路徑下的檔案,而非呼叫與a.exe同級目錄下的1.txt;

所對應的等效目錄就是

fin.open("1.txt");      工作路徑\1.txt

而非1.txt真正所在的目錄    模組路徑\1.txt

模組路徑即a.exe所在的路徑了。

所以需要繞個彎

ifstream fin;

char dir[100];

string findir;

getmodulefilename(null,dir, 256); //獲取模組路徑

findir.replace(findir.size()-10,10,"1.txt");

fin.open(findir.c_str());

嗯  , 完美搞定,有問題時再說。

os模組關於路徑

os模組關於路徑的幾個主要方法 os.path 模組路徑訪問函式 os.path.basename 去掉目錄路徑,返回檔名 os.path.dirname 去掉檔名,返回目錄路徑 os.path.split 將路徑分為 dirname,basename 元組os.path.join 將目錄路徑和檔案...

os模組關於路徑

os模組關於路徑的幾個主要方法 os.path 模組路徑訪問函式 os.path.basename 去掉目錄路徑,返回檔名 os.path.dirname 去掉檔名,返回目錄路徑 os.path.split 將路徑分為 dirname,basename 元組 os.path.join 將目錄路徑和檔...

os 模組 python file 與檔案路徑

eg1 root kooxoo20 180 sersync cat test.py usr bin env python print file root kooxoo20 180 sersync python test.py test.py me相對路徑得到的是相對路徑 root kooxoo20 ...