pikapython/examples/os/os_path.py

47 lines
1.5 KiB
Python
Raw Permalink Normal View History

2023-03-26 20:43:51 +08:00
import os
p = os.path
assert p.join('dir', 'file.txt') == 'dir/file.txt'
assert p.join('/home/user', 'dir', 'file.txt') == '/home/user/dir/file.txt'
# Test split method
2023-03-26 21:30:18 +08:00
assert p.split('dir/file.txt')[0] == 'dir'
assert p.split('dir/file.txt')[1] == 'file.txt'
assert p.split('/home/user/dir/file.txt')[0] == '/home/user/dir'
assert p.split('/home/user/dir/file.txt')[1] == 'file.txt'
2023-03-26 20:43:51 +08:00
# Test splitext method
2023-03-26 21:30:18 +08:00
assert p.splitext('file.txt')[0] == 'file'
assert p.splitext('file.txt')[1] == '.txt'
assert p.splitext('/home/user/file.tar.gz')[0] == '/home/user/file.tar'
assert p.splitext('/home/user/file.tar.gz')[1] == '.gz'
2023-03-26 20:43:51 +08:00
# Test basename method
assert p.basename('dir/file.txt') == 'file.txt'
assert p.basename('/home/user/dir/file.txt') == 'file.txt'
# Test dirname method
assert p.dirname('dir/file.txt') == 'dir'
assert p.dirname('/home/user/dir/file.txt') == '/home/user/dir'
# Test exists method
assert p.exists('config/pika_config_void') == False
assert p.exists('/usr/bin') == True
2023-03-26 20:43:51 +08:00
# Test isdir method
assert p.isdir('config/pika_config_void.h') == False
assert p.isdir('config') == True
# Test isfile method
assert p.isfile('config') == False
assert p.isfile('config/pika_config_void.h') == True
# Test isabs method
assert p.isabs('dir/file.txt') == False
assert p.isabs('/home/user/file.txt') == True
# Test abspath method
# assert p.abspath('config/pika_config_void.h') == "/root/pikascript/port/linux/config/pika_config_void.h"
assert p.abspath('/usr/bin') == "/usr/bin"
2023-03-26 20:43:51 +08:00
print("PASS")