Should a directory path variable end with a trailing slash?

前端 未结 12 1982
暖寄归人
暖寄归人 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 04:43

    I know this is an old thread but I thought I'd share what I do. If possible, I'd normally allow for both and do something like this (if it was PHP):

    $fullPath = rtrim($directory, '/') . '/filename.txt');
    

    That way, if the directory is defined in a config file, it doesn't matter whether the next person to change it includes the trailing slash or not.

    0 讨论(0)
  • 2020-12-24 04:43

    Yes, there are lots of filesystems that support files without any extensions, so always add the trailing slash to avoid any problems.

    0 讨论(0)
  • 2020-12-24 04:48

    In php, since dirname(__FILE __) function returns the directory name without a slash at the end. I tend to stick to that convention.

    Otherwise, using a slash at the end of a directory name will conflict with the way dirname(..) works and then you are stuck with handling the two cases since you don't know if the directory name came from a dirname(..) function or a content defined with a trailing slash.

    Bottom Line: Don't use a trailing slash since dirname(..) doesn't.

    // PHP Example
    dirname(__FILE__); // returns c:\my\directory without a trailing slash, so stick to it!
    

    For other languages, check the function that extracts a pathname, and see if it is using a trailing slash or not, then stick to the language's convention.

    0 讨论(0)
  • 2020-12-24 04:56

    I guess this is one of those rare cases where the correct theoretically and practically answer is different.

    It seems @Karl Wilbur's answer for sure is correct in a theoretical sense, as you should be able to distinguish a reference to the directory node itself from the directory's content.

    But in practice I'll argue the correct answer is the opposite:

    • The most important reason is you can tell with certainty that the path /home/FSObjectX/ is a folder, whereas /home/FSObjectX is ambiguous. No one can tell if this is a file of folder.
      Specifications shall always be precise and unambiguous whenever possible.

    • In a vast majority of cases, you will always reference the content of a folder, not the dir node itself.
      In those rare cases where you actually do, it can easily be handled in the code by removing any optional trailing dir-separator.

    • Using double dir-separators will not do any harm, although missing one will for sure.
      In theory a bad argument as your shouldn't code by "chance", but in practice, errors happen, and perhaps using trailing dir-sep might end up with a few fewer runtime errors at an end-user.

    Reading through this interesting thread I haven't found any disadvantages of using trailing dir-sep, only that it's wrong in a theoretical sense. Or did I miss something?

    0 讨论(0)
  • 2020-12-24 05:00

    I tend to just add the trailing slash as I am more than likely going to use that directory to add/retrieve files...

    In terms of web referencing, it can actually increase performance leaving the trailing slash in

    http://www.netmechanic.com/news/vol4/load_no11.htm

    0 讨论(0)
  • 2020-12-24 05:03

    I don't include the trailing slash when I, for example, define a directory for storing files. That is because I will use it like

    $store_file = "$store_path/$file_id";
    

    I will always add a trailing slash before using a variable that's supposed to hold a directory path. I think it's better to always add one than to wonder if the trailing slash is included.

    0 讨论(0)
提交回复
热议问题