os.getcwd()

get the current working directory

import os

print(os.getcwd()) # get the current working directory
'''
out:
/Users/wuxinyi/Desktop
[Finished in 80ms]
'''

os.chdir('path')

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]
'''

os.listdir()

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]
'''

os.mkdir('...')

只能创建一级目录

import os
os.chdir('/Users/wuxinyi/Desktop/test/')

os.mkdir('OS-Demo-2')
print(os.listdir())
'''
out:
['OS-Demo-2']
'''

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/4ba4b708-0bb7-4051-a160-d85781e6d8f0/Untitled.png

os.makedirs('...')

可以创建多层级

import os
os.chdir('/Users/wuxinyi/Desktop/test/')

os.makedirs('OS-Demo-1/Sub-Dir-1')
print(os.listdir())
'''
out:
['OS-Demo-1']
'''

https://s3-us-west-2.amazonaws.com/secure.notion-static.com/09d9fc14-79f4-46af-b201-0648ae8eeca8/Untitled.png

os.rmdir('...')