python 判断目录、文件夹、文件是否存在,创建目录、文件夹、文件

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命令,创建文件

猜你喜欢

转载自blog.csdn.net/zxcasd11/article/details/103763392