python创建和删除目录的方法


Posted in Python onApril 29, 2015

本文实例讲述了python创建和删除目录的方法。分享给大家供大家参考。具体分析如下:

下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录

#--------------------------------------
#      Name: create_directory.py
#     Author: Kevin Harris
# Last Modified: 02/13/04
#  Description: This Python script demonstrates
#         how to create a single
#         new directory as well as delete a directory
#         and everything 
#         it contains. The script will fail 
#         if encountewrs a read-only
#         file
#--------------------------------------
import os
#--------------------------------------
# Name: deleteDir()
# Desc: Deletes a directory and its content recursively.
#--------------------------------------
def deleteDir( dir ):
  for name in os.listdir( dir ):
    file = dir + "/" + name
    if not os.path.isfile( file ) and os.path.isdir( file ):
      deleteDir( file ) # It's another directory - recurse in to it...
    else:
      os.remove( file ) # It's a file - remove it...
  os.rmdir( dir )
#--------------------------------------
# Script entry point...
#--------------------------------------
# Creating a new directory is easy...
os.mkdir( "test_dir" )
# Pause for a moment so we can actually see the directory get created.
input( 'A directory called "tes_dir" was created.\n\nPress Enter to delete it.' )
# Deleting it can be a little harder since it may contain files, so we'll need 
# to write a function to help us out here.
deleteDir( "test_dir" );

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
跟老齐学Python之有容乃大的list(1)
Sep 14 Python
Python使用正则匹配实现抓图代码分享
Apr 02 Python
python文件写入实例分析
Apr 08 Python
Python正则表达式使用经典实例
Jun 21 Python
使用python获取csv文本的某行或某列数据的实例
Apr 03 Python
python脚本监控Tomcat服务器的方法
Jul 06 Python
python tkinter实现彩球碰撞屏保
Jul 30 Python
Django中create和save方法的不同
Aug 13 Python
Python面向对象编程基础实例分析
Jan 17 Python
Python流程控制语句的深入讲解
Jun 15 Python
如何使用python记录室友的抖音在线时间
Jun 29 Python
Python实现加密的RAR文件解压的方法(密码已知)
Sep 11 Python
python访问系统环境变量的方法
Apr 29 #Python
python中迭代器(iterator)用法实例分析
Apr 29 #Python
在Python中使用HTMLParser解析HTML的教程
Apr 29 #Python
python安装以及IDE的配置教程
Apr 29 #Python
python获取从命令行输入数字的方法
Apr 29 #Python
在Python中处理XML的教程
Apr 29 #Python
python搜索指定目录的方法
Apr 29 #Python
You might like
php获取中文拼音首字母类和函数分享
2014/04/24 PHP
ThinkPHP中order()使用方法详解
2016/04/19 PHP
JavaScript使用技巧精萃[代码非常实用]
2008/11/21 Javascript
url 编码 js url传参中文乱码解决方案
2010/04/11 Javascript
js 上传图片预览问题
2010/12/06 Javascript
仿中关村在线首页弹出式广告插件(jQuery版)
2012/05/03 Javascript
实例详解jQuery结合GridView控件的使用方法
2016/01/04 Javascript
[原创]jQuery常用的4种加载方式分析
2016/07/25 Javascript
浅谈实现vue2.0响应式的基本思路
2018/02/13 Javascript
nuxt踩坑之Vuex状态树的模块方式使用详解
2019/09/06 Javascript
Angular单元测试之事件触发的实现
2020/01/20 Javascript
js回调函数原理与用法案例分析
2020/03/04 Javascript
JavaScript冒泡算法原理与实现方法深入理解
2020/06/04 Javascript
python删除列表中重复记录的方法
2015/04/28 Python
使用Python编写简单的画图板程序的示例教程
2015/12/08 Python
Python常用内置模块之xml模块(详解)
2017/05/23 Python
如何高效使用Python字典的方法详解
2017/08/31 Python
用Python删除本地目录下某一时间点之前创建的所有文件的实例
2017/12/14 Python
Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)
2018/04/05 Python
学生信息管理系统Python面向对象版
2019/01/30 Python
解决Jupyter NoteBook输出的图表太小看不清问题
2020/04/16 Python
Python读写压缩文件的方法
2020/07/30 Python
把富文本的回车转为br标签
2019/08/09 HTML / CSS
html5中canvas学习笔记1-画板的尺寸与实际显示尺寸
2013/01/06 HTML / CSS
美国专注于健康商品的网站:eVitamins
2017/01/23 全球购物
会计专业毕业生自我评价
2013/09/25 职场文书
记帐员岗位责任制
2014/02/08 职场文书
幼儿园毕业家长感言
2014/02/10 职场文书
简单租房协议书
2014/04/09 职场文书
《北大荒的秋天》教学反思
2014/04/14 职场文书
超市活动计划书
2014/04/24 职场文书
篮球比赛口号
2014/06/10 职场文书
公司开除员工通知
2015/04/22 职场文书
保护校园环境倡议书
2015/04/28 职场文书
JavaScript 中for/of,for/in 的详细介绍
2021/11/17 Javascript
详解jQuery的核心函数和事件处理
2022/02/18 jQuery