pyfilesystem2 icon indicating copy to clipboard operation
pyfilesystem2 copied to clipboard

Question - how to interact with Windows OSFS paths?

Open danizen opened this issue 3 years ago • 3 comments

Please see issue palewire/django-bakery#156. In one commit, I changed a couple places where that code uses os.path.join() for a path that will be validated by PyFilesystem2. However, there is still a validation issue with paths that start with C:/ and it is not clear to me how the abstraction is supposed to work.

The issue is that when the BUILD_DIR is set to a path such as "C:\Users\davisda4\workdir\build", then the path is not valid according to fs.path.validatepath. The error occurs when checking whether the path exists at https://github.com/palewire/django-bakery/blob/master/bakery/views/base.py#L62.

In this code, self.fs is an OSFS filesystem initialized from "osfs:///", which may not be valid on Windows.

danizen avatar Mar 01 '22 16:03 danizen

PyFilesystem is always expecting paths with forward slashes, and you should not use fs.path with filesystem paths. In theory, the idea would be that you do:

import os
import fs

with fs.open_fs(os.getenv("BUILD_DIR")) as build_dir:
    build_dir.makedirs("/the/dir/to/make")

So in your example:

    def prep_directory(self, target_dir):
        """
        Prepares a new directory to store the file at the provided path, if needed.
        """
        dirname = path.dirname(target_dir)
        if dirname:
            with fs.open_fs(settings.BUILD_DIR) as build_dir:
                if not build_dir.exists(dirname):
                    logger.debug("Creating directory at {}".format(build_dir.getsyspath(dirname)))
                    build_dir.makedirs(dirname)

althonos avatar Mar 26 '22 17:03 althonos

@althonos Presumably that last line in your example should have been build_dir.makedirs(dirname) ?

lurch avatar Apr 01 '22 00:04 lurch

Yes indeed, edited.

althonos avatar Apr 01 '22 10:04 althonos