os Module API Document
API
def mkdir(self,path:str,*mode):...
def rmdir(self,path:str):...
def chdir(self,path:str):...
def listdir(self,path:str)->list:...
def getcwd(self)->str:...
def open(self,filename:str,flags:int)->FILE:...
def read(self,fd:FILE,len:int)->str:...
def write(self,fd:FILE,buf:any)->int:...
def lseek(self,fd:FILE,pos:int,how:int)->int:...
def close(self,fd:FILE):...
def fstat(self,fd:FILE)->fileStat:...
def remove(self,filename:str):...
def rename(self,old:str,new:str):...
class fileStat:
def st_size(self)->int:...
class path:
def join(self,*paths)->str:...
def split(self,path:str)->tuple:...
def splitext(self,path:str)->tuple:...
def basename(self,path:str)->str:...
def dirname(self,path:str)->str:...
def exists(self,path:str)->bool:...
def isdir(self,path:str)->bool:...
def isfile(self,path:str)->bool:...
def isabs(self,path:str)->bool:...
def abspath(self,path:str)->str:...
Examples
os_path.py
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
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'
# Test splitext method
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'
# 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
# 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"
print("PASS")
os_test1.py
import os
origin = os.getcwd()
os.chdir("test/out")
os.getcwd()
try:
# cleanup
os.remove("_testdir/testfile")
os.rmdir("_testdir")
except:
pass
os.mkdir("_testdir")
os.chdir("_testdir")
f = os.open("testfile", os.O_CREAT | os.O_RDWR)
assert os.write(f, b"Hello World!") == 12
assert os.lseek(f, 0, 0) == 0
print(os.read(f, 100))
os.close(f)
os.chdir("..")
os.chdir(origin)
os.listdir('.')
print("PASS")