1

Here's how I'm currently guessing whether a path leads to a theoretical file or folder:

def __init__(self, path:str, is_dir:bool=None):
    # checks
    if is_dir is None: is_dir = path.endswith('/') or path.endswith('\\') or os.path.isdir(path) or '.' not in path  # guess if path is pointing to a file
    self.is_dir = is_dir
    # note: Because both files and folders can, but don't have to include dots in their names,
    #       this check is just a basic guess.
    #
    #       files without extensions that were not yet created will be recognised as folders
    #       and
    #       folders with dots in their name that were not yet created will be recognised as files
    #
    #       If you know of a better solution to this problem (which I frankly don't think exists),
    #       please implement it here.

Is there a better way that works on both UNIX-likes and Windows? (and possibly even elsewhere?)

(in case that the question is confusing: by theoretical I mean that it might not exist (yet))

Johnystar
  • 156
  • 5
  • use os library os.isdir, some googling.... https://stackoverflow.com/questions/955941/how-to-identify-whether-a-file-is-normal-file-or-directory-using-python also, ending in a / or \ does not mean that a path is a directory – E.Serra Jun 04 '20 at 08:40
  • Is there any problem with your code ? – Maurice Meyer Jun 04 '20 at 08:46
  • @E.Serra that does not answer my question: I want to be able to guess whether a path **would** be a directory or a file. I already am using os.path.isdir in case the directory already exists, but what if it doesn't? What if the path doesn't exist at all (yet)? As far as I'm aware, there is no way to actually do this sadly, but I'm at least looking for some advice. – Johnystar Jun 04 '20 at 08:48
  • @MauriceMeyer I'm looking if there's a better way to guess whether a path leads to a directory or a file (even if it doesn't exist yet). – Johnystar Jun 04 '20 at 08:50
  • Do you create the path manually or are they created by `os.*` like `os.path.join(x, y)` or `os.walk(...)` ? – Maurice Meyer Jun 04 '20 at 08:50
  • 1
    @MauriceMeyer All I'm trying to do (and I know it really doesn't make much sense) is to tell whether the non-existent path would lead to a file or directory. While directories usually don't have dots in them and vice versa, I'm looking if there's any way to answer "the question" with 100% confidence. – Johnystar Jun 04 '20 at 08:54
  • 1
    being a directory is an attribute of the file, so no there is no way before it exists to tell, direcctories don't need to end with / \ etc, files don't need to have a 'format' etc. you can tell if something will NEVER be a file, but not if it will be a directory, your aproach seems to be as good as it gets. – E.Serra Jun 04 '20 at 08:58
  • @E.Serra That's what I figured. Also, you can only put a backward slash in a filename on Linux from what I've been able to test. On Windows, you can't put either in a file. – Johnystar Jun 04 '20 at 09:09

1 Answers1

-2

Overall, this question is indeed pretty dumb, however, there are a few pointers that can help you guess (not determine):

  • on common Windows filesystems, you cannot put \ or / in a file (both are accepted as separators in a path)
  • on most common Linux filesystems, however, you can put a \ in a file (/ is used as a separator in a path)
  • directories are less likely to contain dots in their name (this isn't the case for "hidden" directories on Linux, also you can easily name a directory something like why.would.you.do.this)

If you're still desperate for better confidence:

  • you could check the path against a list of common file extensions (this will still, however, only provide you with a guess)
  • reconsider your life decisions

I'm sorry for asking such a dumb question.

Johnystar
  • 156
  • 5