我可以使用哪个Python库从路径中提取文件名,而不管操作系统或路径格式是什么?
例如,我希望所有这些路径都返回c:
a/b/c/
a/b/c
\a\b\c
\a\b\c\
a\b\c
a/b/../../a/b/c/
a/b/../../a/b/c
我可以使用哪个Python库从路径中提取文件名,而不管操作系统或路径格式是什么?
例如,我希望所有这些路径都返回c:
a/b/c/
a/b/c
\a\b\c
\a\b\c\
a\b\c
a/b/../../a/b/c/
a/b/../../a/b/c
当前回答
如果您的文件路径不是以“/”结尾,且目录以“/”分隔,则使用以下代码。众所周知,path通常不以“/”结尾。
import os
path_str = "/var/www/index.html"
print(os.path.basename(path_str))
但在某些情况下,像url以“/”结尾,然后使用以下代码
import os
path_str = "/home/some_str/last_str/"
split_path = path_str.rsplit("/",1)
print(os.path.basename(split_path[0]))
但是当你的路径被“\”分开时,你通常在Windows路径中找到,然后你可以使用以下代码
import os
path_str = "c:\\var\www\index.html"
print(os.path.basename(path_str))
import os
path_str = "c:\\home\some_str\last_str\\"
split_path = path_str.rsplit("\\",1)
print(os.path.basename(split_path[0]))
您可以通过检查操作系统类型将两者组合成一个函数并返回结果。
其他回答
在python 3.4或更高版本中,使用pathlib。路径:
>>> from pathlib import Path
>>> Path("/tmp/d/a.dat").name
'a.dat'
.name属性将给出路径中最后一个子元素的全名,而不管它是文件还是文件夹。
如果您的文件路径不是以“/”结尾,且目录以“/”分隔,则使用以下代码。众所周知,path通常不以“/”结尾。
import os
path_str = "/var/www/index.html"
print(os.path.basename(path_str))
但在某些情况下,像url以“/”结尾,然后使用以下代码
import os
path_str = "/home/some_str/last_str/"
split_path = path_str.rsplit("/",1)
print(os.path.basename(split_path[0]))
但是当你的路径被“\”分开时,你通常在Windows路径中找到,然后你可以使用以下代码
import os
path_str = "c:\\var\www\index.html"
print(os.path.basename(path_str))
import os
path_str = "c:\\home\some_str\last_str\\"
split_path = path_str.rsplit("\\",1)
print(os.path.basename(split_path[0]))
您可以通过检查操作系统类型将两者组合成一个函数并返回结果。
我在Windows和Ubuntu (WSL)上使用此方法,它只使用“import os”即可工作(我): 基本上,replace()根据当前操作系统平台设置正确的路径分隔符。
如果路径以斜杠'/'结束,那么它不是一个文件而是一个目录,因此它返回一个空字符串。
import os
my_fullpath = r"D:\MY_FOLDER\TEST\20201108\20201108_073751.DNG"
os.path.basename(my_fullpath.replace('\\',os.sep))
my_fullpath = r"/MY_FOLDER/TEST/20201108/20201108_073751.DNG"
os.path.basename(my_fullpath.replace('\\',os.sep))
my_fullpath = r"/MY_FOLDER/TEST/20201108/"
os.path.basename(my_fullpath.replace('\\',os.sep))
my_fullpath = r"/MY_FOLDER/TEST/20201108"
os.path.basename(my_fullpath.replace('\\',os.sep))
在Windows(左)和Ubuntu(通过WSL,右)上:
为了完整起见,这里是python 3.2+的pathlib解决方案:
>>> from pathlib import PureWindowsPath
>>> paths = ['a/b/c/', 'a/b/c', '\\a\\b\\c', '\\a\\b\\c\\', 'a\\b\\c',
... 'a/b/../../a/b/c/', 'a/b/../../a/b/c']
>>> [PureWindowsPath(path).name for path in paths]
['c', 'c', 'c', 'c', 'c', 'c', 'c']
这在Windows和Linux上都适用。
像其他人建议的那样使用os.path.split或os.path.basename并不能在所有情况下工作:如果您在Linux上运行脚本并试图处理经典的windows样式的路径,它将失败。
Windows路径可以使用反斜杠或正斜杠作为路径分隔符。因此,ntpath模块(相当于os. path)在windows上运行时的路径)将适用于所有平台上的所有(1)路径。
import ntpath
ntpath.basename("a/b/c")
当然,如果文件以斜杠结束,basename将为空,所以创建自己的函数来处理它:
def path_leaf(path):
head, tail = ntpath.split(path)
return tail or ntpath.basename(head)
验证:
>>> paths = ['a/b/c/', 'a/b/c', '\\a\\b\\c', '\\a\\b\\c\\', 'a\\b\\c',
... 'a/b/../../a/b/c/', 'a/b/../../a/b/c']
>>> [path_leaf(path) for path in paths]
['c', 'c', 'c', 'c', 'c', 'c', 'c']
(1) There's one caveat: Linux filenames may contain backslashes. So on linux, r'a/b\c' always refers to the file b\c in the a folder, while on Windows, it always refers to the c file in the b subfolder of the a folder. So when both forward and backward slashes are used in a path, you need to know the associated platform to be able to interpret it correctly. In practice it's usually safe to assume it's a windows path since backslashes are seldom used in Linux filenames, but keep this in mind when you code so you don't create accidental security holes.