当前位置:网站首页>os. path. Pits encountered during the use of join()
os. path. Pits encountered during the use of join()
2022-06-24 08:23:00 【go_ flush】
- linux The way to splice paths is a slash /, win Concatenated backslash \
- I am here os.path.join() It was found that If there is a slash or backslash at the beginning of the spliced path The splice path is truncated from the slash or backslash (win In the system ), If it is linux System The slash will be truncated , Backslash does not .
- The code example is as follows :
win System
a_path_win = "http://www.baidu.com"
b_path_win = "data/img"
path_join = os.path.join(a_path_win, b_path_win) # The way to search the path is the backslash \
print(f"sys = {
sys.platform}")
print(f"path_join_win = {
path_join}")
Printed results
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}")
Printed results
path_join_win_1 = /data/img # Truncate at slash
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}")
Printed results
path_join_win_2 = \data\img # Truncate at backslash
Linux System
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}")
Printed results
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}")
Printed results
path_join_1= /data/img # Slashes are also truncated
d_path = "\data\img"
path_join_2 = os.path.join(a_path, d_path)
print(f"path_join_2 = {
path_join_2}")
Printed results
path_join_2 = http://www.baidu.com/\data\img # Backslashes do not truncate , They can splice
FAQ
os.path.join stay win The backslash and slash encountered in the system are truncated here , stay linux Slashes encountered under the system will be truncated , Backslash does not , Do you have to remove the slash or backslash at the beginning when splicing paths , Otherwise, the path cannot be found
边栏推荐
猜你喜欢

jwt(json web token)

The first exposure of Alibaba cloud's native security panorama behind the only highest level in the whole domain

Swift 基础 闭包/Block的使用(源码)

How to use the virtual clock of FPGA?

Utilisation de la fermeture / bloc de base SWIFT (source)

Swift foundation features unique to swift

Swift 基础 Swift才有的特性

Leetcode 207: course schedule (topological sorting determines whether the loop is formed)

1279_ Vsock installation failure resolution when VMware player installs VMware Tools

2021-03-11 COMP9021第八节课笔记
随机推荐
问题3 — messageBox弹框,修改默认背景色
Tool functions – get all files in the project folder
Model effect optimization, try a variety of cross validation methods (system operation)
LINQ 查询(2)
[008] filter the table data row by row, jump out of the for cycle and skip this cycle VBA
Methods of vector operation and coordinate transformation
MAYA重新拓布
Vscode topic recommendation
Introduction to software engineering - Chapter 2 - feasibility study
软件过程与项目管理期末复习与重点
Nodejs redlock notes
Which is the first poem of Tang Dynasty?
DHCP, TFTP Foundation
问题4 — DatePicker日期选择器,2个日期选择器(开始、结束日期)的禁用
2022 mobile crane driver special operation certificate examination question bank and online simulation examination
Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)
Installation and use of selenium IDE
Sql语句内运算问题
2021-03-04 COMP9021第六节课笔记
You get in Anaconda