Invocation of relative file paths in Python

Two sessions in May | NVIDIA DLI Deep Learning Introductory Course

May 19/May 26 One- 640?wx_fmt=jpeg&wxfrom=5&wx_lazy=1 day intensive study to get you started quickly Read more >


The main text is 624 words in total, and the estimated reading time is 3 minutes.


foreword


Let's first look at a problem with function calls that use relative file paths. Suppose there are now two script files main.py and func.py, and their path relationship is:


1.
2|--dir1
3    |--main.py
4    |--dir2
5       |--func.py
6       |--test.txt


The function of func.py is to provide the load_txt() function, read the contents of the test.txt file in the same directory and return it.


1# func.py
2def load_txt()
3filename = './test.txt'
4return open(filenamem, 'r').read()


Suppose now the load_txt() function is called in main.py:


1# main.py
2from dir2 import func
3if __name__ == '__main__':
4print func.load_txt()


At this time, an error similar to the file test.txt not found will be reported.


Why is this so? This is because in the process of calling the function, the current path. represents the path where the executed script file is located. In this case, . represents the path where main.py is located, so the load_txt() function will look for the test.txt file in the dir1 folder.


So how can we keep the relative path unchanged during the function call?


method


There are quite a few tutorials on the Internet that mention this relative file path problem in Python, but most of them don't mention the solution in this case.


In the following three functions, the first and second are the solutions in most tutorials, but this is wrong because the "current file path" obtained by the first and second functions is The path of the executed script file, only the current file path returned by the third function is the real path of the script file where the function is located


1def get_cur_path1():
2import os
3return os.path.abspath(os.curdir)
4def get_cur_path2():
5import sys
6return sys.argv[0]
7def get_cur_path3():
8import os
9return os.path.dirname(__file__)


Therefore, the solution is as follows. Modify the read function in func.py as follows:


1# func.py
2import os
3def load_txt()
4module_path = os.path.dirname(__file__)    
5filename = modelu_path + '/test.txt'
6return open(filenamem, 'r').read()


Original link: https://www.jianshu.com/p/cd421014cfbb


For more concise and convenient classified articles and the latest course and product information, please move to the newly presented "LeadAI Academy Official Website":

www.leadai.org


Please pay attention to the artificial intelligence LeadAI public account to view more professional articles

640?wx_fmt=jpeg

Everyone is watching

640.png?

Application of LSTM model in question answering system

TensorFlow-based neural network solves the problem of user churn overview

The Most Common Algorithm Engineer Interview Questions (1)

Sorting out the most common algorithm engineer interview questions (2)

TensorFlow from 1 to 2 | Chapter 3 The Beginning of the Deep Learning Revolution: Convolutional Neural Networks

Decorators | Advanced Programming in Python

Why don't you review the basics of Python today?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325994285&siteId=291194637