Home  >  Article  >  Backend Development  >  How to get the file name in python

How to get the file name in python

(*-*)浩
(*-*)浩Original
2019-06-28 11:23:3214479browse

Python can obtain the current file name through __file__ or sys.argv[0]. The following takes the test.py file as an example.

How to get the file name in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn