如何获取执行当前线程的文件的当前路径?

时间:2015-10-06 00:07:02

标签: python multithreading

在python中,我可以这样做以获取当前文件的路径:

os.path.dirname(os.path.abspath(__file__))

但如果我在一个主题上运行这个说:

def do_stuff():
   class RunThread(threading.Thread):
       def run(self):
           print os.path.dirname(os.path.abspath(__file__))
   a = RunThread()
   a.start()

我收到此错误:

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
    self.run()
  File "readrss.py", line 137, in run
    print os.path.dirname(os.path.abspath(__file__))
NameError: global name '__file__' is not defined

2 个答案:

答案 0 :(得分:2)

int main()
{
//Declare
    int rows = 0, cols = 0, maxVal = 0;
    ifstream infile("image.pgm");
    string inputLine = "";
    string trash = "";

    //First line "P5"
    getline(infile,inputLine);

    //ignore lines with comments
    getline(infile,trash);
    while (trash[0] == '#')
    {
        getline(infile,trash);
    }
    //get the rows and cols
    istringstream iss(trash);
    getline(iss, inputLine, ' ');
    rows = atoi(inputLine.c_str());
    getline(iss, inputLine, ' ');
    cols = atoi(inputLine.c_str());
    //get the last plain text line maxval
    getline(infile,inputLine);
    maxVal = atoi(inputLine.c_str());

    //Now start reading individual bites



    Matrix<int, rows, cols> m;

    //now comes the data
    for(i = 0; i<rows; i++)
    {
        for(j = 0; j < cols; j++)
        {
            //store data into matrix
        }
    }




    system("Pause");
    return 0;
}

inspect

答案 1 :(得分:-1)

我为之前的回答道歉。我半睡半醒,回答得很愚蠢。 每当我完成你想要做的事情时,我就按照相反的顺序使用它。例如。 os.path.abspath(os.path.dirname(__file__))