Python 如何从文件路径中获取文件名

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() 返回,该函数接受一个路径参数。

示例

以下程序使用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] - 匹配集合内的单词

  • +? - 如果在?关键字之前只出现一次,则匹配该字符串

  • (?=) - 匹配任何没有换行符的字符,请记住停在。

正则表达式re.search()方法

Python正则表达式re.search()方法在目标字符串中搜索正则表达式模式的所有出现,并返回匹配的Match对象实例。只返回目标字符串中的第一个与模式匹配的结果。

示例

以下程序使用正则表达式(regex)从给定的文件路径中返回文件名:

# 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

结论

在本文中,我们学习了如何使用三种不同的方法从给定的文件路径中获取文件名。我们学习了如何使用操作系统模块的内置功能来修改提供的文件路径,以满足我们的需求。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程