如何在 Python 中从文件路径获取文件名?
在本文中,我们将学习一个 Python 程序,用于从文件路径获取文件名。
使用的方法
以下是完成此任务的各种方法:
使用 OS 模块函数
使用 Pathlib 模块
使用正则表达式(regex 模块)
方法 1:使用 OS 模块函数
使用 split() 函数从文件路径获取文件名
split() 函数将字符串拆分为列表。我们可以定义分隔符;默认分隔符是任何空格。
算法(步骤)
以下是执行所需任务应遵循的算法/步骤:
使用 import 关键字导入 os 模块。
创建一个变量来存储输入文件路径。
使用split() 函数根据 '/' 分隔符将文件路径拆分为单词列表。
使用负索引(从末尾开始索引,即 -1、-2 等)从列表中获取最后一个元素。
打印结果文件名。
示例
以下程序使用 split() 函数从给定的文件路径返回文件名:
# importing os module
import os
# input file path
inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# Printing the given file path
print("The given file path is:",inputFilepath)
# splitting the file path into a list of words based on '/' and
# getting the last element using negative indexing
print("The File Name is:\n",os.path.basename(inputFilepath).split('/')[-1])
输出
执行后,以上程序将生成以下输出:
The given file path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf The File Name is: tutorialsPoint.pdf
使用 os.path.basename 从文件路径获取文件名
使用内置 Python 函数os.path.basename(),可以确定指定路径中的基本名称。函数path.basename()返回路径名 path 的基本名称,它接受一个 path 参数。
示例
以下程序使用 os.path.basename() 函数从给定的文件路径返回文件名:
# importing os module
import os
# input path of the file
inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# Printing the given input path
print("Give Input Path is:",inputFilepath)
# getting the last component(main file name )of the input file path
print("The File Name is:\n",os.path.basename(inputFilepath))
输出
执行后,以上程序将生成以下输出:
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf The File Name is: tutorialsPoint.pdf
使用 os.splitext() 从文件路径获取文件名
如果我们只需要没有扩展名的文件名或仅扩展名,此方法将生成文件及其扩展名。这里,os 模块的 splitext 函数发挥作用。
os.splitext() 方法将返回一个字符串元组,其中包含文件名和内容,我们可以使用索引访问它们。
示例
以下程序使用 os.splitext() 函数从给定的文件路径返回文件名:
# importing os module
import os
inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# Printing the given input path
print("Give Input Path is:",inputFilepath)
# getting the file name from the file path
fileName = os.path.basename(inputFilepath)
# splitting the file using the splittext() function
full_file = os.path.splitext(fileName)
# printing the tuple of a string containing file name and extension separately
print(full_file)
# Concatenating file name with file extension using string concatenation
print("The File Name is:\n",full_file[0] + full_file[1])
输出
Give Input Path is: C:/Users/cirus/Desktop/tutorialsPoint.pdf
('tutorialsPoint', '.pdf')
The File Name is:
tutorialsPoint.pdf
方法 2:使用 Pathlib 模块
Python Pathlib 模块中提供了多个类,这些类描述了具有适合多种操作系统的语义的文件系统路径。此模块是 Python 基本实用程序模块之一。
如果我们想要带有扩展名的文件,我们可以使用 name 属性,即使stem是允许从链接中提取不带扩展名的文件名的实用程序属性之一。
示例
以下程序使用Path() 函数和 Pathlib 模块的 stem 属性从给定的文件路径返回文件名:
# importing Path from pathlib module
from pathlib import Path
# input file path
inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# getting the filename from the file path
# here the stem attribute extracts the file name from filepath
print('File Name is:',Path(inputFilepath).stem)
# here the name attribute returns full name(along with extension)
# of the input file
print("The File Name Along with Extension is:",Path(inputFilepath).name)
输出
File Name is: tutorialsPoint The File Name Along with the Extension is: tutorialsPoint.pdf
方法 3:使用正则表达式(regex 模块)
为了使用特定模式匹配文件名,我们可以使用正则表达式。
pattern - [\w]+?(?=\.)
上述模式分为 3 个模式:
[\w] - 匹配集合内的单词
+? - 如果在 ? 关键字之前只出现一次,则匹配字符串
(?=) - 匹配任何字符(不包括换行符),并请记住停止在。
regex re.search() 方法
Python regex re.search() 方法在整个目标字符串中搜索 regex 模式的出现,并在找到匹配项的位置返回相应的 Match Object 实例。re.search() 只返回目标字符串中第一个与模式匹配的匹配项。
示例
以下程序使用正则表达式从给定的文件路径返回文件名:
# importing re(regex) module
import re
# input file path
inputFilepath = 'C:/Users/cirus/Desktop/tutorialsPoint.pdf'
# regex pattern to extract the file name
regex_pattern = '[\w-]+?(?=\.)'
# searching/matching the pattern of the input file path
result = re.search(regex_pattern, inputFilepath)
# printing the match name
print("The File Name is:",result.group())
输出
The File Name is: tutorialsPoint
结论
在本文中,我们学习了如何使用三种不同的方法从给定的文件路径获取文件名。我们学习了如何使用 OS 模块的内置功能修改提供的文件路径以满足我们的需求。
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP