当前位置:网站首页>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 系统下遇到斜杠会截断,反斜杠不会,在拼接路径时一定要把开头处的斜杠或者反斜杠处理掉吗,要不然会出现找不到路径的情况
边栏推荐
- LeetCode练习——跳跃游戏、组合求和
- 工控机防破解
- Oracle advanced SQL qualified query
- 5-if语句(选择结构)
- "Adobe international certification" about Adobe Photoshop, creating and modifying brush tutorials?
- 软件工程导论——第二章——可行性研究
- decltype用法介绍
- 有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
- On the H5 page, the Apple phone blocks the content when using fixed to locate the bottom of the tabbar
- Latest news of awtk: new usage of grid control
猜你喜欢

对于flex:1的详细解释,flex:1

Part 1: building OpenGL environment

Swift Extension ChainLayout(UI的链式布局)(源码)

The monthly salary of two years after graduation is 36K. It's not difficult to say
![[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA](/img/a0/f03b8d9c8f5e53078c38cce11f8ad3.png)
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA
![[nilm] non intrusive load decomposition module nilmtk installation tutorial](/img/d0/bc5ea1cbca9ee96a2fe168484ffec4.png)
[nilm] non intrusive load decomposition module nilmtk installation tutorial

Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV

Keep one decimal place and two decimal places

Vulnhub target: boredhackerblog: social network
![[C language] system date & time](/img/de/faf397732bfa4920a8ed68df5dbc48.png)
[C language] system date & time
随机推荐
Dart development server, do I have a fever?
Basics of reptile B1 - scrapy (learning notes of station B)
Saccadenet: use corner features to fine tune the two stage prediction frame | CVPR 2020
Hilbert Huang Transform
Introduction to software engineering - Chapter 2 - feasibility study
Chapter 2 line graph of canvas
Installation and use of selenium IDE
Latest news of awtk: new usage of grid control
【点云数据集介绍】
5-if语句(选择结构)
Open cooperation and win-win future | Fuxin Kunpeng joins Jinlan organization
Using kubeconfig files to organize cluster access
[data update] Xunwei comprehensively upgraded NPU development data based on 3568 development board
C# Lambda
VR is destined to reappear in the Jianghu?
Chapter 1 overview of canvas
Auto usage example
第 2 篇:繪制一個窗口
蓝桥杯_N 皇后问题
第 1 篇:搭建OpenGL环境