问题:python中如果存在目录或文件就删除目录或文件
解决:使用os.path
方法:
import os
upload_dir = os.path.realpath(os.path.join(os.path.dirname(__file__),
"../static/uploads/attach"))
if not os.path.exists(upload_dir):
os.makedirs(upload_dir)
if os.path.exists(upload_dir):
os.rmdir(upload_dir)
fname = self.get_argument('fname','')
org_id = self.current_user.org_id
school_dir = os.path.realpath(os.path.join(os.path.dirname(__file__),
"../static/uploads/attach/%d"%org_id))
file_path = os.path.join(school_dir, fname)
if os.path.exists(file_path):
os.remove(file_path)