import os # 导入os库
os.getcwd() 获取当前工作目录
cwd = os.getcwd() # 获取当前工作目录
print("Current working directory: {}".format(cwd)) # 打印当前工作目录。花括号和其中的字符将替换为传递给 str.format() 方法的对象,花括号中的数字可用来表示传递给 str.format() 方法的对象的位置
os.path.join() 正确创建文件路径
根据操作系统的规则正确连接一个或多个路径
cwd = os.getcwd() # 获取当前工作目录
new_folder = 'new_folder' # 当前工作目录下的子文件夹
path = os.path.join(cwd, new_folder)
print( f"已创建目录: {path}") # 打印目录。f"{变量名}"用于在字符串中插入变量的值
os.listdir() 列出目录和文件
接受文件路径并返回该路径中的文件和文件夹的列表
path = '/' # 获取根目录下文件和文件夹的列表
dir_list = os.listdir(path)
print(dir_list) # 打印列表
参考网页:
https://www.zhihu.com/question/31843617/answer/2561417012
https://docs.python.org/zh-cn/3.7/tutorial/index.html