os模块操作文件、路径,是python比较常用的一个库,简要介绍,原文见Python os模块实例。
# I use jupyter notebook to create some file
!touch foo.txt
!echo Hello > foo.txt
!cat foo.txt
Hello
# rename file
os.rename('foo.txt','bar.txt')
!cat bar.txt
Hello
# remove file
os.remove('bar.txt')
改变目录
# current dir
print os.getcwd() # current working directory
/Users/yongle/OMOOC2py/cheat
# go down
os.chdir('img')
print os.getcwd()
# go back up
os.chdir(os.pardir) #or simply os.chdir('..')
print os.getcwd()
/Users/yongle/OMOOC2py/cheat/img
/Users/yongle/OMOOC2py/cheat
遍历目录listdir
# listdir