pyfilesystem2
pyfilesystem2 copied to clipboard
Invalid characters in the path
I apologize if my question sounds silly but I just started using this library and can't find the answer to my question. I'm trying to copy a file from my disk system to a memory file system:
mem_fs = fs.open_fs('mem://')
zip_path = 'C:\\project\\test.zip"
fs.copy.copy_file('/', zip_path.format(os.getcwd()), mem_fs, 'test.zip')
I have the following exception: fs.errors.InvalidCharsInPath because the path contains ":" and "\". If I replace all these "invalid" characters with "/" the resource can't be found. The OS: Win7. How can this issue be solved? And what is the cross-platform solution (I will run my app on Linux too)? Thanks!
Couple things:
- Your arguments are ordered wrong (see here)
- You need to pass in an OSFS as the second file system, or a path to the zip file like "osfs://C:\project\test.zip"
@dargueta thanks for your response. Unfortunately, the documentation is pretty vague.
I don't understand from there about the first argument "src_fs (FS or str) – Source filesystem (instance or URL)." what does it mean? What should be passed here? Is the second argument wrong? The documentation says "Path to a file on the source filesystem.". Should it be prefixed with "osfs"? Where can I read about it or see a full example?
I also don't understand your second point. What is "OSFS" as "a second file system"? Is it about the second argument?
Note: I copied my code from this solution https://stackoverflow.com/questions/62247379/extract-tar-file-inside-memory-filesystem, is that wrong too?
Thanks!
Okay, I figured it out. The working code:
os_fs = fs.open_fs('osfs://d:/project))
mem_fs = fs.open_fs('mem://')
mem_fs.makedir('zip')
zip_file = 'test.zip'
fs.copy.copy_file(os_fs, zip_file, mem_fs, 'zip/test.zip')
But I think the documentation should have examples.
If you're trying to extract the contents of your zipfile to an in-memory filesystem, there's no need to copy the zipfile itself to the memfs first, you could just do something like:
mem_fs = MemoryFS()
with ZipFS('C:\\project\\test.zip') as zip_fs:
fs.copy.copy_fs(zip_fs, mem_fs)
@lurch Wow I didn't know there is an easy way to extract the content of a zip file into a memory file system, this is superhelpful, thanks!
Oh, I thought you were trying to copy the zip file itself. Sorry I misled you!
@dargueta I was trying to copy just because I didn't know that extracting can be done in other, simpler way. Also, I needed the copying anyway, so this thread turned out to be very helpful.
I am having this same issue, using jupyter-fs on Windows.
This is because os.path.join
is being used to create the path for OSFS.islink()
etc., which inserts backslashes on windows, and which are deemed invalid by fs.
To get fs to behave nicely with commonly used os.path
manipulation such as os.path.join
, it should probably accept backslashes as a separator on Windows.
I have raised this issue with jupyter-fs, but the os.path.join pattern is very ingrained in Python coders, so it might be worth accommodating that. It would also give an easier route to adoption of PyFilesystem.
@PaulKGrimes : PyFilesystem abstracts away from paths, and for any path manipulation there's fs.path
which has all the path handling functions you may want with the behaviour expected by PyFilesystem. It's up to the filesystem implementation to handle the conversion, so that you can use forward slashes everywhere and use fs.path.join
.
@PaulKGrimes Pleas see the docs on PyFilesystem paths.