get the current working directory
import os
print(os.getcwd()) # get the current working directory
'''
out:
/Users/wuxinyi/Desktop
[Finished in 80ms]
'''
change the current working directory to path
import os
print(os.getcwd()) # get the current working directory
os.chdir('/Users/wuxinyi/Desktop/test/')
print(os.getcwd())
'''
out:
/Users/wuxinyi/Desktop
/Users/wuxinyi/Desktop/test
[Finished in 59ms]
'''
list the directory
import os
print(os.getcwd()) # get the current working directory
print(os.listdir())
'''
out:
/Users/wuxinyi/Desktop
['Thumbs.db', '.DS_Store', 'untitled1.py', 'test', '.localized', '学校概况.pdf', '$RECYCLE.BIN', 'test.py', 'Hands-On-Machine-Learning-with-Scikit-Learn-Keras-and-Tensorflow_-Concepts-Tools-and-Techniques-to-Build-Intelligent-Systems-O’Reilly-Media-2019.pdf', '.texpadtmp', '华大手册.md', '.ipynb_checkpoints', '礼仪文化.xmind', 'desktop.ini', 'python_tutorial.py', '毕设']
[Finished in 75ms]
'''
只能创建一级目录
import os
os.chdir('/Users/wuxinyi/Desktop/test/')
os.mkdir('OS-Demo-2')
print(os.listdir())
'''
out:
['OS-Demo-2']
'''
可以创建多层级
import os
os.chdir('/Users/wuxinyi/Desktop/test/')
os.makedirs('OS-Demo-1/Sub-Dir-1')
print(os.listdir())
'''
out:
['OS-Demo-1']
'''