从文件路径中删除 ../ 或 ./
Remove ../ or ./ from file path
我有一个文件路径和一个目录路径。该文件被支持osed 到该目录中的某处(多个级别)。我想将文件路径的开头与目录路径进行比较。所以我基本上做的是:
if file_path.startswith(directory_path):
do_something()
两个路径都是字符串。不幸的是,我的文件路径包括“..”和“.”。所以它看起来像这样:/home/user/documents/folder/../pictures/house.jpg
。由于另一条路径不包含 those 个点,显然比较失败。 python 中有没有办法从字符串中删除第 os 个点?我想使用 os 模块中的 path.join() ,但没有用。
非常感谢您的帮助:)
Is there a way in python to remove those spots from the string? I thought of using path.join() from the os module, which did not work. Thanks a lot for any help :)
os.path.abspath
将规范化路径并将其绝对化。或者,pathlib.Path.resolve()
.
我有一个文件路径和一个目录路径。该文件被支持osed 到该目录中的某处(多个级别)。我想将文件路径的开头与目录路径进行比较。所以我基本上做的是:
if file_path.startswith(directory_path):
do_something()
两个路径都是字符串。不幸的是,我的文件路径包括“..”和“.”。所以它看起来像这样:/home/user/documents/folder/../pictures/house.jpg
。由于另一条路径不包含 those 个点,显然比较失败。 python 中有没有办法从字符串中删除第 os 个点?我想使用 os 模块中的 path.join() ,但没有用。
非常感谢您的帮助:)
Is there a way in python to remove those spots from the string? I thought of using path.join() from the os module, which did not work. Thanks a lot for any help :)
os.path.abspath
将规范化路径并将其绝对化。或者,pathlib.Path.resolve()
.