python创建目录,删除目录,删除文件

本文共有723个字,关键词:

问题: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)
版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。
添加新评论
暂无评论