当前位置:网站首页>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 系统下遇到斜杠会截断,反斜杠不会,在拼接路径时一定要把开头处的斜杠或者反斜杠处理掉吗,要不然会出现找不到路径的情况
边栏推荐
- C# Lambda
- 有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
- decltype用法介绍
- 3-list introduction
- 4-operation list (loop structure)
- On the H5 page, the Apple phone blocks the content when using fixed to locate the bottom of the tabbar
- Pipeline concept of graphic technology
- Introduction to software engineering - Chapter 3 - Requirements Analysis
- Analysis of abnormal problems in domain name resolution in kubernetes
- Chrono usage notes
猜你喜欢
随机推荐
Auto usage example
蓝桥杯_N 皇后问题
Upgrade Mysql to the latest version (mysql8.0.25)
首次曝光 唯一全域最高等级背后的阿里云云原生安全全景图
Chapter 2 line graph of canvas
L1-019 who goes first (15 points)
The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain
Vulnhub靶机:BOREDHACKERBLOG_ CLOUD AV
快速读论文----AD-GCL:Adversarial Graph Augmentation to Improve Graph Contrastive Learning
1279_VMWare Player安装VMWare Tools时VSock安装失败解决
VR is destined to reappear in the Jianghu?
Unity culling related technologies
The monthly salary of two years after graduation is 36K. It's not difficult to say
Specify IP when calling feign interface
Chapter 1 overview of canvas
UTC、GMT、CST
FPGA的虚拟时钟如何使用?
没有专业背景,还有机会成为机器学习工程师吗?
有关iframe锚点,锚点出现上下偏移,锚点出现页面显示问题.iframe的srcdoc问题
Chapter 3 curve graph of canvas







![[run the script framework in Django and store the data in the database]](/img/6b/052679e5468e5a90be5c4339183f43.png)

