当前位置:网站首页>os.path.join()使用过程中遇到的坑
os.path.join()使用过程中遇到的坑
2022-06-24 06:56:00 【go_flush】
- linux 路径的拼接的方式是斜杠/, win 的拼接反斜杠\
- 我在os.path.join() 使用过程中发现 如果被拼接的路径开头存在斜杠或者反斜杠 拼接路径会从斜杠或者反斜杠处截断(win系统中),如果是linux 系统 则斜杠会截断,反斜杠不会。
- 代码实例如下:
win 系统
a_path_win = "http://www.baidu.com"
b_path_win = "data/img"
path_join = os.path.join(a_path_win, b_path_win) # 路径的搜索方式是反斜杠\
print(f"sys = {
sys.platform}")
print(f"path_join_win = {
path_join}")
打印的结果
sys = win32
path_join_win = http://www.baidu.com\data/img
c_path_win = "/data/img"
path_join_win_1 = os.path.join(a_path_win, c_path_win)
print(f"path_join_win_1 = {path_join_win_1}")
打印的结果
path_join_win_1 = /data/img # 在斜杠处截断
d_path_win = "\data\img"
path_join_win_2 = os.path.join(a_path_win, d_path_win)
print(f"path_join_win_2 = {
path_join_win_2}")
打印的结果
path_join_win_2 = \data\img # 在反斜杠处截断
Linux 系统
a_path = "http://www.baidu.com"
b_path = "data/img"
path_join = os.path.join(a_path, b_path)
print(f"{
sys.platform}")
print(f"path_join = {
path_join}")
打印的结果
linux
path_join = http://www.baidu.com/data/img
c_path = "/data/img"
path_join_1 = os.path.join(a_path, c_path)
print(f"path_join_1= {
path_join_1}")
打印的结果
path_join_1= /data/img # 斜杠同样会截断
d_path = "\data\img"
path_join_2 = os.path.join(a_path, d_path)
print(f"path_join_2 = {
path_join_2}")
打印的结果
path_join_2 = http://www.baidu.com/\data\img #反斜杠不会截断,会拼接
FAQ
os.path.join 在win系统下遇到反斜杠和斜杠都在此处截断,在linux 系统下遇到斜杠会截断,反斜杠不会,在拼接路径时一定要把开头处的斜杠或者反斜杠处理掉吗,要不然会出现找不到路径的情况
边栏推荐
- 【资料上新】迅为基于3568开发板的NPU开发资料全面升级
- Online education fades
- 1-4metasploitable2介绍
- 有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
- 【点云数据集介绍】
- Solution of electric education system for intelligent supervision station
- 毕业两年月薪36k,说难也不难吧
- In the post epidemic era, the home service robot industry has just set sail
- Basics of reptile B1 - scrapy (learning notes of station B)
- Hilbert Huang Transform
猜你喜欢
[nilm] non intrusive load decomposition module nilmtk installation tutorial
Selenium IDE的安装以及使用
More than observation | Alibaba cloud observable suite officially released
直播回顾 | 云原生混部系统 Koordinator 架构详解(附完整PPT)
搜索与推荐那些事儿
Screenshot recommendation - snipaste
Case examples of corpus data processing (cases related to sentence retrieval)
OC extension detects whether an app is installed on the mobile phone (source code)
How to cancel the display of the return button at the uniapp uni app H5 end the autobackbutton does not take effect
Examples of corpus data processing cases (reading multiple text files, reading multiple files specified under a folder, decoding errors, reading multiple subfolder text, batch renaming of multiple fil
随机推荐
【资料上新】迅为基于3568开发板的NPU开发资料全面升级
Serialization of unity
"Adobe international certification" about Adobe Photoshop, creating and modifying brush tutorials?
对于flex:1的详细解释,flex:1
Solve the problem of notebook keyboard disabling failure
Chapter 3 curve graph of canvas
C语言_字符串与指针的爱恨情仇
Atguigu---15- built in instruction
These dependencies were not found: * core JS / modules / es6 array. Fill in XXX
You get in Anaconda
Introduction of model compression tool based on distiller
More than observation | Alibaba cloud observable suite officially released
解决错误: LNK2019 无法解析的外部符号
Standing at the center of the storm: how to change the engine of Tencent
Cold thinking on the hot track: multiplier effect is the fundamental requirement of East West calculation
Introduction to software engineering - Chapter 2 - feasibility study
Swift Extension ChainLayout(UI的链式布局)(源码)
Phonics
VsCode主题推荐
[run the script framework in Django and store the data in the database]