python获取文件名和扩展名的方法

1. 采用切片法提取文件名和扩展名

创建一个变量,文件名。假设它有一个三个字母的扩展名,并使用切片操作,找到扩展名。对于 README.txt,扩展名应该是 txt。使用切片操作编写代码,将给出没有扩展名的名称。您的代码是否适用于任意长度的文件名?

您的代码是:

filename = “readme.txt”
extension = filename[-3:]
print(“The extension is”, extension)
name = filename[:len(filename)-4]
print(“The name without extension is”, name)

2. os.path 和 pathlib方法提取文件名和扩展名

在 Python 中,有不同的方法可以创建一个变量并找到文件名的扩展名。一种方法是使用 os.path 模块,它提供了一个名为 splitext() 的函数,它可以将文件路径分割为文件名和文件扩展名另一种方法是使用 pathlib 模块,它有一个 Path 类,它有一个 suffix 属性,可以用来获取文件扩展名。这里有一些使用这些方法的代码示例:

使用 os.path.splitext():import os
filename = “README.txt” # 创建一个变量,包含文件名
name, extension = os.path.splitext(filename) # 分割文件名和扩展名
print(“The extension is”, extension) # 打印扩展名
print(“The name without extension is”, name) # 打印没有扩展名的名称

使用 pathlib.Path().suffix:from pathlib import Path
filename = “README.txt” # 创建一个变量,包含文件名
file_path = Path(filename) # 创建一个 Path 对象
extension = file_path.suffix # 获取扩展名
print(“The extension is”, extension) # 打印扩展名
name = file_path.stem # 获取没有扩展名的名称
print(“The name without extension is”, name) # 打印没有扩展名的名称

这两种方法都适用于任意长度的文件名,只要它们有一个点(.)分隔文件名和扩展名。如果文件名没有点,或者有多个点,结果可能会根据使用的方法而不同。例如,如果文件名是 “.bashrc”,os.path.splitext() 方法会返回 (“.bashrc”, “”) 作为文件名和扩展名,而 pathlib.Path().suffix 方法会返回 “” 作为扩展名。类似地,如果文件名是 “foo.bar.tar.gz”,os.path.splitext() 方法会返回 (“foo.bar.tar”, “.gz”) 作为文件名和扩展名,而 pathlib.Path().suffix 方法会返回 “.gz” 作为扩展名。如果你想获取一个有多个点的文件名中的所有扩展名,你可以使用 pathlib.Path().suffixes 属性,它会返回一个扩展名的列表。例如,pathlib.Path(“foo.bar.tar.gz”).suffixes 会返回 [“.bar”, “.tar”, “.gz”]。


Create a variable, filename. Assuming that it has a three letter extension, and using slice operations, find the extension. For README.txt, the extension should be txt. Write code using slice operations that will give the name without the extension. Does your code work on filenames of arbitrary length?

filename = "readme.txt"
extension = filename[-3:]
print(extension)
name = filename[:len(filename)-4]
print(name)

There are different ways to create a variable and find the extension of a filename in Python. One way is to use the os.path module, which provides a function called splitext() that splits the file path into the file name and the file extension1. Another way is to use the pathlib module, which has a Path class that has a suffix property that can be used to get the file extension2. Here are some examples of code using these methods:

Using os.path.splitext():

import os
filename = "README.txt" # create a variable with the file name
name, extension = os.path.splitext(filename) # split the file name and extension
print("The extension is", extension) # print the extension
print("The name without extension is", name) # print the name without extension

Using pathlib.Path().suffix:from pathlib

import Path
filename = "README.txt" # create a variable with the file name
file_path = Path(filename) # create a Path object
extension = file_path.suffix # get the extension
print("The extension is", extension) # print the extension
name = file_path.stem # get the name without extension
print("The name without extension is", name) # print the name without extension

Both of these methods work on filenames of arbitrary length, as long as they have a dot (.) separating the name and the extension. If the filename does not have a dot, or has more than one dot, the results may vary depending on the method used. For example, if the filename is “.bashrc”, the os.path.splitext() method will return (“.bashrc”, “”) as the name and extension, while the pathlib.Path().suffix method will return “” as the extension. Similarly, if the filename is “foo.bar.tar.gz”, the os.path.splitext() method will return (“foo.bar.tar”, “.gz”) as the name and extension, while the pathlib.Path().suffix method will return “.gz” as the extension. If you want to get all the extensions in a filename with multiple dots, you can use the pathlib.Path().suffixes property, which returns a list of extensions2. For example, pathlib.Path(“foo.bar.tar.gz”).suffixes will return [“.bar”, “.tar”, “.gz”].

程序运行结果:

txt readme


The extension is .txt
The name without extension is README


The extension is .txt
The name without extension is README