Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

which one should I use: os.sep or os.path.sep?

They are same, but which one should I use?

http://docs.python.org/library/os.html:

os.sep

The character used by the operating system to separate pathname components. This is '/' for POSIX and '\' for Windows. Note that knowing this is not sufficient to be able to parse or concatenate pathnames — use os.path.split() and os.path.join() — but it is occasionally useful. Also available via os.path.

like image 788
zhigang Avatar asked Aug 01 '11 15:08

zhigang


People also ask

What is os path SEP?

os. path. sep is the character used by the operating system to separate pathname components.

What does os path mean?

The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats.

What is os path Islink?

islink() method in Python is used to check whether the given path represents an existing directory entry that is a symbolic link or not. Note: If symbolic links are not supported by the Python runtime then os.

Is Python a path os?

path. isdir() method in Python is used to check whether the specified path is an existing directory or not. This method follows symbolic link, that means if the specified path is a symbolic link pointing to a directory then the method will return True.


1 Answers

I'd use os.path.sep to make it very clear that it's the path separator… But consistency is more important, so if one is already being used, use that. Otherwise, pick one and use it all the time.

Edit: Just to make sure you're not reinventing the wheel, though, the path module already has join, split, dirname, and basename functions… So you should rarely need to use path.sep:

>>> os.path.join("foo", "bar", "baz") 'foo/bar/baz' >>> os.path.split(_) ('foo/bar', 'baz') 
like image 51
David Wolever Avatar answered Oct 05 '22 15:10

David Wolever