I want to write a simple unit test with os.path.basename
def test(path):
return os.path.basename(os.path.normpath(title))
...
result = test('/path/foo')
assert result == 'foo'
result = test(r'c:\Users\Foo\Documents\foo')
assert result == 'foo'
Running on linux the second test (windows path) is failing.
I guess the first test will fail on windows.
This actually makes pretty much sense since there are different os.path
modules
Since different operating systems have different path name conventions, there are several versions of this module in the standard library.
Is there a way to import a specific version of os.path
?
I already tried to set sys.platform
to win32
Of course I could check the current platform and just run one of both tests - but I was wondering if there is a way to run both tests.
You should always use /
as the path separator. Python will translate it to the correct separator for your platform.
\\
is used to start escape sequences. Using /
will avoid having to use raw strings to disable \\
escape sequences (as in the second string in your post).
Using \\
in raw strings may also cause os.path
methods to behave oddly (and will only work on Windows platforms).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.