pyfilesystem2 icon indicating copy to clipboard operation
pyfilesystem2 copied to clipboard

fs.errors.CreateFailed: root path 'C:\pyfilesytem_test_dir\DJI_0355.JPG' does not exist

Open tomaslindstrom opened this issue 3 years ago • 6 comments

Hi,

The issue I have is that I get the errormessage : "fs.errors.CreateFailed: root path 'C:\pyfilesytem_test_dir\DJI_0355.JPG' does not exist" when using getinfo. I have tried several ways to specify the path, but still get the issue. I provide samle code below.

Sample 1

  import fs
   
  class WindowsFileHandler:
     def __init__(self, file_path):
         self.file_path = file_path

 
     def extract_filename_and_path(self):
         with fs.open_fs('osfs://' + self.file_path) as filesystem:
             if filesystem.exists(self.file_path):
                 # Extract the filename and path
                 file_info = filesystem.getinfo(self.file_path)
                 filename = file_info.name
                 directory = file_info.parent
                 return filename, directory
             else:
                 return None, None

if __name__ == '__main__':
   # Replace 'C:/path/to/your/file' with the actual path to the file on your Windows system.
   file_path = r'C:\pyfilesytem_test_dir\DJI_0355.JPG'
   file_handler = WindowsFileHandler(file_path)
   filename, directory = file_handler.extract_filename_and_path()
   
   if filename and directory:
       print("File Name:", filename)
       print("Directory Path:", directory)
   else:
       print("File not found.")

Sample 2

`import fs
from fs import open_fs
path = 'C:\\test_dir\\DJI_0355.JPG'  # Use double backslashes or raw string for Windows path
with open_fs(path) as filesystem:
    if filesystem.exists(path):
        # Extract the filename and path
        filename = filesystem.getinfo(path).name
        directory = filesystem.getinfo(path).parent
        print("File Name:", filename)
        print("Directory Path:", directory)
    else:
        print("File not found.")

Both codesnippets gets the following result: "fs.errors.CreateFailed: root path 'C:\pyfilesytem_test_dir\DJI_0355.JPG' does not exist"

Event though the path and file exists.

Thanks in advance for support or suggestions!

tomaslindstrom avatar Oct 12 '23 07:10 tomaslindstrom

You need to open the filesystem using a directory-name, not a file-name. And once you've got an FS-object, you need to give it relative pyfilesystem-style paths, not absolute Windows-style paths. So (I haven't tested this, because I don't use Windows) I think you'd want something like:

from fs import open_fs
my_path = 'C:\\test_dir'  # Use double backslashes or raw string for Windows path
my_file = 'DJI_0355.JPG'
with open_fs(my_path) as filesystem:
    if filesystem.exists(my_file):
        # Extract the filename and path
        filename = filesystem.getinfo(my_file).name
        directory = filesystem.getinfo(my_file).parent
        print("File Name:", filename)
        print("Directory Path:", directory)
    else:
        print("File not found.")

And to show how directories inside pyfilesystem work:

from fs import open_fs
my_path = 'C:\\'  # Use double backslashes or raw string for Windows path
my_file = 'test_dir/DJI_0355.JPG'
with open_fs(my_path) as filesystem:
    if filesystem.exists(my_file):
        # Extract the filename and path
        filename = filesystem.getinfo(my_file).name
        directory = filesystem.getinfo(my_file).parent
        print("File Name:", filename)
        print("Directory Path:", directory)
    else:
        print("File not found.")

lurch avatar Oct 12 '23 17:10 lurch

Thanks for advice!

I have tested - I did get some progress from them, but still gets the error: AttributeError: 'Info' object has no attribute 'parent' with the both code snippets.

The statement: filename = filesystem.getinfo(my_file).name now now perfectly fine for me.

Will continue to try, to find out why I'm not getting the expected result. Please give any suggestions.

tomaslindstrom avatar Oct 16 '23 13:10 tomaslindstrom

.parent isn't a supported property. See https://docs.pyfilesystem.org/en/latest/info.html#info for more information. https://docs.pyfilesystem.org/en/latest/reference/path.html#fs.path.dirname might be what you're looking for.

lurch avatar Oct 16 '23 13:10 lurch

The fs.path.dirname will solve it for me -thanks!

I got the .parent solution from a opencv class at Linkdin learning, and also from several ChatGPT code suggestions. I was confused about this, since I also got the impression from the docs that it was not supported, so thanks for confirming this. I guess it could be the general issue with ChatGPT code that it is trained on old versions of libarires that might have changed up to now.

tomaslindstrom avatar Oct 17 '23 08:10 tomaslindstrom

Can the issue be closed now? There's obviously nothing that the PyFilesystem developers can do about ChatGPT :laughing:

lurch avatar Oct 17 '23 09:10 lurch

Issue closed, thanks for support. /Tomas

tomaslindstrom avatar Oct 17 '23 10:10 tomaslindstrom