1

Let's say I have 2 variables dir_name = mydir and file_name = file1, each to be used in formatted strings. There're a few ways to insert a / in a string with those 2 variables.

  1. dir_name = mydir/; file_name = file1
  2. dir_name = /mydir; file_name = /file1
  3. dir_name = mydir; file_name = file1

With Python, there are 3 common ways to create format strings: concatenation, f strings and .format(). When using concatenation, options 1 and 2 makes it so that we don't need to concat an extra / which may be more readable. With f strings and .format() though, I find not having the / in the names makes it slightly more readable with something like f"{dir_name}/{file_name}" or "{}/{}".format(dir_name, file_name) which clearly has a separating / in it instead of a bunch of variables squashed together.

Between options 1 and 2, option 1 makes it clear which variable is a directory and which is a file. Option 2 though, feels a bit more uniform to me.

I know this is a small issue but I've found it to have a slight impact on code readability for me, mainly in Python with a lot of directories.

Is there a standard way to insert the / in directory and file name strings?

  • 9
    Even if you use a coding convention, always use os.path.join() (or similar in other languages) so it doesn't matter. – user949300 Apr 23 '21 at 02:58
  • Sometimes join() method is not applicable (is an URL, or Unix path on Windows). It is better then to rely on typesafe representation of path as a list of segments, where each segment if forbidden to have delimiters. There are libraries for that. – Basilevs Apr 23 '21 at 05:57
  • 1
    You should really be using the Path library and its overloaded / operator, so that the separator doesn't have be stored anywhere and no string formatting is necessary. – Kilian Foth Apr 23 '21 at 06:04
  • @Basilevs 1) The question asked about file and directory substrings, not URLs; urlparse.urljoin exists for that purpose. 2) At least in Python, posixpath and ntpath exist if you're dealing with a specific filesystem. – Philip Kendall Apr 23 '21 at 10:36

3 Answers3

9

Relative paths are not strings. They are relative paths. They should be represented as some sort of Path object, not a string.

The path separator between relative paths is not part of either path, so it should not be present in either.

The proper way to resolve those relative paths, is to use a path resolver.

Python has a library in the standard library for this, so do many other languages.

Jörg W Mittag
  • 103,514
4

Standardization is better than custom tailoring, but what's even better is just outright solving the problem and precluding the need for standardization.

C# (and .Net in general) has solved this by providing the Path.Combine method, which takes in any amount of strings and concatenates them to form a single path, while also handling the / and \ characters inbetween the strings (so there are no doubles or missing slashes).

This has dramatically simplified the string juggling that path generation entails, and I would personally advise anyone to rely on a similar reusable method.

If your language doesn't provide it (edit: and there's no adequate third party library that provides it), write it yourself. It's not very hard to implement (just some endsWith and startsWith logical checks), and it will pay back dividends by no longer needing to either check for clashes or enforce some sort of universal standard that everyone must comply with.

Flater
  • 49,580
2

dir_name = /mydir; file_name = /file1

This approach is definitely wrong, and has led to disaster.

"/file1" does contain a directory name, and that directory is the root directory. So if you accidentally use it with an empty dir_name, it will refer to the root directory. This is a good way to accidentally destroy the system.

pjc50
  • 13,377
  • 1
  • 31
  • 35