python 判断目录、文件夹、文件是否存在,创建目录、文件夹、文件
判断目录或文件夹是否存在
import os
folder_or_dir = "~/work"
if not os.path.exists(folder_or_dir):
print("folder or dir not exist !!!!!!")
os.makedirs(folder_or_dir) # 创建目录或文件夹
判断文件是否存在
import os
file_name = "~/test.png"
if not os.path.exists(file_name):
print("file not exist !!!!!!")
os.system("touch test.png") # 调用shell命令,创建文件
本文介绍如何使用Python的os模块来判断目录、文件夹或文件是否存在,并提供了创建目录、文件夹及文件的方法。通过示例代码,读者可以学习到如何在Python中进行基本的文件系统操作。
1012

被折叠的 条评论
为什么被折叠?



