python - python os path join 似乎在 win 和 OSX OS 之间无法正常工作
问题描述
我遇到了一段在 OSX 和 Windows 上运行的简单代码的问题。我正在使用 Python 3.7.9。
我所做的只是将路径链接到文件名;我读取了一个目录内容,然后创建了打开这个文件的完整路径
路径是相对的;images_to_rename
我在所在的同一目录中运行 python 脚本
from os import path, listdir
# Dir where the images are
assets = "images_to_rename"
# create a list of files in that dir
filelist = listdir(assets)
for file in filelist:
# join path and file name
joined_name = path.join(assets, file)
print(joined_name)
假设该文件夹具有panorama.png
和house.png
。当我在 Windows 上打印时,我得到images_to_rename\\panorama.png
,这是错误的,因为它应该是images_to_rename\panorama.png
. path.join
如果无法在 win 和 OSX 上正确使用相同的代码,则不确定使用的意义何在。
我可以通过在调用时用替换\\
替换来解决,但我希望避免使用路径连接。我错过了什么吗?\
path.join
解决方案
推荐阅读
- python - 如何在 Python 中附加数据框?
- reactjs - 如何渲染相同的组件但具有初始状态(有条件地)?
- ios - 通知在 ios 上不起作用-flutter firebase 消息传递
- sql - SQL中的存储过程检查索引是否存在然后创建
- php - Laravel 路由解析自定义数据类型
- java - 当一个类或接口充当自己的工厂时,是否有选择工厂方法名称的约定/最佳实践?
- android - 华为P20上的安卓图标渲染很奇怪
- java - 从数据库中检索一个值并将其设置为 Spring boot 中的 @Table Name 值
- c# - 托管在 Kestrel (AspNet Core 5.0) 中的 SignalR 服务的 Windows 身份验证
- glsl - 在 Vulkan 中使用 debugPrintfEXT