flying-circus / pyfilesystem

Automatically exported from code.google.com/p/pyfilesystem
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

InvalidCharsInPathError in OSFS for Windows paths #156

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
>>> from fs.osfs import OSFS
>>> os_fs = OSFS('/')
>>> os_fs.getsyspath('foo\bar')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "c:\users\kunal parmar\sources\box\sync\vendor\fs\fs\osfs\__init__.py", line 156, in getsyspath
    self.validatepath(path)
  File "c:\users\kunal parmar\sources\box\sync\vendor\fs\fs\base.py", line 304, in validatepath
    raise InvalidCharsInPathError(path)
fs.errors.InvalidCharsInPathError: Path contains invalid characters: foar
>>> os_fs.getsyspath('foo/bar')
u'\\\\?\\c:\\foo\\bar'

The separator for Windows paths is "\". Therefore, "foo\bar" is not an invalid 
path.

Original issue reported on code.google.com by kunalpar...@gmail.com on 12 Jul 2013 at 5:33

GoogleCodeExporter commented 9 years ago
Pyfilesystem paths use forward slashes, even on Windows. Please read the docs:

http://pythonhosted.org/fs/concepts.html#paths

Original comment by willmcgugan on 12 Jul 2013 at 8:30

GoogleCodeExporter commented 9 years ago
Does that mean that all callers should call fs.path.normpath on all paths 
before calling any API from pyfilesystem? Would it be possible to bake that in 
the API's themselves?

Original comment by kunalpar...@gmail.com on 12 Jul 2013 at 3:47

GoogleCodeExporter commented 9 years ago
If you are working within a Pyfilesystem, then just use forward slashes in 
literals. It ensures everything works cross-platform. Only the OSFS constructor 
takes a path that is in the OS format.

Don't rely on normpath changing backslashes to forwardslashes though. That 
functionality may change in the future. There was a recent-ish discussion in 
the mailing list. The problem is that backslashes are valid path characters in 
Linux and Mac.

Original comment by willmcgugan on 12 Jul 2013 at 4:03

GoogleCodeExporter commented 9 years ago
Nah, I think you've not quite understood how pyfilesystem works ;-)  Try 
re-reading the concepts page Will linked to.
Basically, pyfilesystem is only designed for dealing with paths *within 
pyfilesystem*, rather than being called on system paths (although there are 
functions to get a system path back out of pyfilesystem if needed).
So, use system paths ('c:\foo\bar' etc.) with regular Python functions (e.g. 
from the os.path module), and use pyfilesystem paths ('/foo/bar' etc.) within 
pyfilesystem.
Pyfilesystem is designed to "abstract away" filesystem paths, allowing you to 
treat paths from all the different FS modules (and across all different OSes) 
in the same way.

Original comment by gc...@loowis.durge.org on 12 Jul 2013 at 4:08