How to get the file name in python

(*-*)浩
Release: 2019-07-09 10:17:04
Original
14481 people have browsed it

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]))
Copy after login

Output:

E:/Code/python3/EffectivePython/test.py
E:/Code/python3/EffectivePython/test.py
test.py
test.py
Copy after login

__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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!