[TIP] os-independent file paths

Jonathan Lange jml at mumak.net
Thu Dec 8 05:32:02 PST 2011


On Thu, Dec 8, 2011 at 1:22 PM, Andrea Crotti <andrea.crotti.0 at gmail.com> wrote:
> Testing functions which work on the file system I would like to be able
> to use some paths, both on Windows and on Linux.
>
> Now the problem is that paths are different on the two platforms, so my
> ideas was to have something like
>
> def unix_to_independent_path(unixpath):
>    return path.join(*unixpath.split('/'))
>
>
> And import it where needed as
> import utils.unix_to_independent_path as _
>
> to use like
> _('/very/long/path/')
>

Or, you could do::
  path.split(os.sep)

(This is the inverse function of ``os.path.join(segments)``)

To turn a path into segments. That will work cross-platform. No need
for a special UNIX function.

>
> The problem is that the function doesn't really work for example for
> unix absolute paths
> /very/long/path -> very/long/path.
>
> And in general how would that path convert to a Windows path anyway?
>
> So I guess I should only use relative paths, does it make sense?

It depends on what you're testing, but it sounds like using relative
paths, would be a good plan.

jml



More information about the testing-in-python mailing list