为什么要在字符串串联中使用os.path.join? [英] Why use os.path.join over string concatenation?

查看:285
本文介绍了为什么要在字符串串联中使用os.path.join?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这里看不到大图;但是基本上我不知道为什么要使用os.path.join而不是普通的字符串连接?

I'm not able to see the bigger picture here I think; but basically I have no idea why you would use os.path.join instead of just normal string concatenation?

我主要使用VBScript,所以我不了解此功能的要点.

I have mainly used VBScript so I don't understand the point of this function.

推荐答案

便携式

一次写入文件路径操作,它可在许多不同平台上免费运行.分隔字符被抽象掉,使您的工作更加轻松.

Portable

Write filepath manipulations once and it works across many different platforms, for free. The delimiting character is abstracted away, making your job easier.

如果该目录路径具有

You no longer need to worry if that directory path had a trailing slash or not. os.path.join will add it if it needs to.

使用os.path.join对其他阅读您的代码的人来说,您正在使用文件路径很明显.人们可以快速浏览代码,并从本质上发现它是文件路径.如果您决定自己构造它,则可能使读者无法发现代码的实际问题:嗯,一些字符串连接,一个替代.这是文件路径还是什么?加!他为什么不使用os.path.join?" :)

Using os.path.join makes it obvious to other people reading your code that you are working with filepaths. People can quickly scan through the code and discover it's a filepath intrinsically. If you decide to construct it yourself, you will likely detract the reader from finding actual problems with your code: "Hmm, some string concats, a substitution. Is this a filepath or what? Gah! Why didn't he use os.path.join?" :)

这篇关于为什么要在字符串串联中使用os.path.join?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆