Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid characters in the path #555

Open
kwantdev opened this issue Oct 22, 2022 · 11 comments
Open

Invalid characters in the path #555

kwantdev opened this issue Oct 22, 2022 · 11 comments

Comments

@kwantdev
Copy link

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!

@dargueta
Copy link
Contributor

Couple things:

  1. Your arguments are ordered wrong (see here)
  2. 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"

@kwantdev
Copy link
Author

@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!

@kwantdev
Copy link
Author

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.

@lurch
Copy link
Contributor

lurch commented Oct 23, 2022

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)

@kwantdev
Copy link
Author

@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!

@dargueta
Copy link
Contributor

Oh, I thought you were trying to copy the zip file itself. Sorry I misled you!

@kwantdev
Copy link
Author

@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.

@PaulKGrimes
Copy link

PaulKGrimes commented Dec 1, 2022

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.

@PaulKGrimes
Copy link

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.

@althonos
Copy link
Member

althonos commented Dec 2, 2022

@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.

@willmcgugan
Copy link
Member

@PaulKGrimes Pleas see the docs on PyFilesystem paths.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants