Search code examples
javascriptphppathcall

Do all file paths follow the same principles / conventions across different programming languages


One of the biggest challenges when starting programming I've found is getting to grips with file paths. For instance when you reference a path in a folder that is at the same level in the directory structure you'll see either:

/folder/style.css or

folder/style.css etc.

Are the principles the same across all programming languages when you're referencing different files, and are there any naming conventions or gotchas to look out for?

Why is it sometimes you'll see a single dot './' and sometimes a double dot '../' when you're moving up a level?

I can't seem to find any tutorials about this, and I'm guessing it's probably one of those things that is bog-standard on a computer science course, but very hard to get concrete info on if you're self-taught/ teaching yourself.

Emily.


Solution

  • File path conventions are not the same in different languages.

    Older languages like C simply had no conventions at all. They merely exposed APIs from the OS. For example, an absolute path on Unix-like OSes could be /usr/chewy/stuff.txt and on Windows it would be C:\Users\chewy\stuff.txt.

    They did this primarily for two reasons: first, it was the easier thing to do and second (and more importantly) is to let users use the conventions they've learned on their OS when using a program written in the language. A unix user would not expect to need to enter Windows style paths and similarly a Windows user would expect file paths to look like Windows file paths.

    In some newer languages the default file path is the Unix style /a/b/c. They still support Windows style paths but users can also enter unix style paths on Windows and it would still be valid (in most cases / maps to C:\ unless explicitly changed like F:/ would map to F:\).

    In some even newer languages go even further. They use URI syntax for file paths so internally you'd see file://home/chewy/stuff.txt. This evolved almost simultaneously from two directions: web based software like web browsers and the Plan 9 operating system (admittedly Plan 9 was probably influenced by web development as well).

    Relative Paths

    How languages handle relative paths depend on what type of convention it supports. For languages that have no convention relative path works the way it works on your OS. So for Windows you need to learn Windows file path conventions and Unix you'd need to learn unix file path conventions.

    For languages that internally standardize on unix style file paths they'd also implement unix file path conventions. However, not all languages implement all the standard unix conventions. For example, Tcl supports ~ to refer to the user's home directory but most other languages don't.

    For languages that use URI syntax you need to learn how URI handle relative paths. It's actually covered by the RFC defining URI syntax: https://www.rfc-editor.org/rfc/rfc3986

    Common conventions

    The .. file refers to the parent directory on Unixen, Windows and even in URI syntax. So you'll see this often regardless of language or OS.

    The . at the beginning of a path refers to the current working directory on Unix and Windows. In URI syntax a . is simply removed from the path.

    Further reading

    I'd strongly suggest learning about file path conventions of your OS. Ten years ago I wouldn't not have expected anyone asking this question because if they can access a website then they'd know how to navigate the filesystem. These days I rarely see Windows users access cmd.com (is it still called cmd.com?).

    In addition I'd also suggest learning about URI syntax because this is one of the things I often see beginners misunderstand.