Should a directory path variable end with a trailing slash?

前端 未结 12 2043
暖寄归人
暖寄归人 2020-12-24 04:33

When defining a path to a directory as a variable or constant, should it end with a trailing slash? What is the convention?

pwd in Unix shows your curre

12条回答
  •  情歌与酒
    2020-12-24 05:08

    Maybe you should think about what your decision would mean for files. If you don't include the trailing slash at the end of a directory name you'll have to add it to the start of the file name.

    Now, if for some reason, the path leading up to the file is missing when you concatenate strings, you end up with something like /filename which is not just a file but an absolute path from the root directory (wherever that may be in that context).

    That's why I end my paths with a slash and keep files as files.

提交回复
热议问题