Home >Backend Development >Python Tutorial >How to get the file name in python
Python can obtain the current file name through __file__ or sys.argv[0]. The following takes the test.py file as an example.
test.py: ( Recommended learning: Python video tutorial)
# -*- coding: utf-8 -*- # test.py import sys import os # 绝对路径 print(__file__) print(sys.argv[0]) # 文件名 print(os.path.basename(__file__)) print(os.path.basename(sys.argv[0]))
Output:
E:/Code/python3/EffectivePython/test.py E:/Code/python3/EffectivePython/test.py test.py test.py
__file__ and sys.argv[0] are both current The absolute path of the file. The file name can be obtained through os.path.basename.
For more Python-related technical articles, please visit the Python Tutorial column to learn!
The above is the detailed content of How to get the file name in python. For more information, please follow other related articles on the PHP Chinese website!