Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
686 views
in Technique[技术] by (71.8m points)

python - How/where to use os.path.sep?

os.path.sep is the character used by the operating system to separate pathname components.

But when os.path.sep is used in os.path.join(), why does it truncate the path?

Example:

Instead of 'home/python', os.path.join returns '/python':

>>> import os
>>> os.path.join('home', os.path.sep, 'python')
'/python'

I know that os.path.join() inserts the directory separator implicitly.

Where is os.path.sep useful? Why does it truncate the path?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Where os.path.sep is usefull?

I suspect that it exists mainly because a variable like this is required in the module anyway (to avoid hardcoding), and if it's there, it might as well be documented. Its documentation says that it is "occasionally useful".

Why it truncates the path?

From the docs for os.path.join():

If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.

and / is an absolute path on *nix systems.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...