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 dict remove数组删除(del,pop)
Mar 24 Python
python中对list去重的多种方法
Sep 18 Python
python修改字典内key对应值的方法
Jul 11 Python
python验证码识别的示例代码
Sep 21 Python
python并发2之使用asyncio处理并发
Dec 21 Python
python实现m3u8格式转换为mp4视频格式
Feb 28 Python
numpy的文件存储.npy .npz 文件详解
Jul 09 Python
python列表使用实现名字管理系统
Jan 30 Python
Python实现的排列组合、破解密码算法示例
Apr 12 Python
简单易懂Pytorch实战实例VGG深度网络
Aug 27 Python
python selenium自动化测试框架搭建的方法步骤
Jun 14 Python
解析python 类方法、对象方法、静态方法
Aug 15 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
迅雷下载《中学科技》怀旧期刊下载
2021/02/27 无线电
php下清空字符串中的HTML标签的代码
2010/09/06 PHP
Jquery之Ajax运用 学习运用篇
2011/09/26 Javascript
javascript实现yield的方法
2013/11/06 Javascript
JS动态添加与删除select中的Option对象(示例代码)
2013/12/20 Javascript
JS delegate与live浅析
2013/12/21 Javascript
使用JQuery实现Ctrl+Enter提交表单的方法
2015/10/22 Javascript
jQuery实现动态给table赋值的方法示例
2017/07/04 jQuery
AngularJS ng-repeat指令及Ajax的应用实例分析
2017/07/06 Javascript
详解Vuejs2.0 如何利用proxyTable实现跨域请求
2017/08/03 Javascript
客户端(vue框架)与服务器(koa框架)通信及服务器跨域配置详解
2017/08/26 Javascript
React中jquery引用的实现方法
2017/09/12 jQuery
使用Bootstrap和Vue实现用户信息的编辑删除功能
2017/10/25 Javascript
vue中如何实现pdf文件预览的方法
2018/07/12 Javascript
vue使用ajax获取后台数据进行显示的示例
2018/08/09 Javascript
js实现点击展开隐藏效果(实例代码)
2018/09/28 Javascript
javascript实现文本框标签验证的实例代码
2018/10/14 Javascript
C#程序员入门学习微信小程序的笔记
2019/03/05 Javascript
微信小程序云开发之云函数详解
2019/05/16 Javascript
JavaScript 面向对象基础简单示例
2019/10/02 Javascript
python使用在线API查询IP对应的地理位置信息实例
2014/06/01 Python
Python日志模块logging简介
2015/04/13 Python
实例解析Python中的__new__特殊方法
2016/06/02 Python
Python tkinter实现的图片移动碰撞动画效果【附源码下载】
2018/01/04 Python
Python解决抛小球问题 求小球下落经历的距离之和示例
2018/02/01 Python
pycharm中import呈现灰色原因的解决方法
2020/03/04 Python
完美解决keras保存好的model不能成功加载问题
2020/06/11 Python
Travelstart沙特阿拉伯:廉价航班、豪华酒店和实惠的汽车租赁优惠
2019/04/06 全球购物
工程管理造价应届生求职信
2013/11/13 职场文书
十岁生日父母答谢词
2014/01/18 职场文书
庆国庆活动总结
2014/08/28 职场文书
2014年药店店长工作总结
2014/11/17 职场文书
2016年小学“公民道德宣传日”活动总结
2016/04/01 职场文书
情况说明书格式及范文
2019/06/24 职场文书
PostgreSQL 插入INSERT、删除DELETE、更新UPDATE、事务transaction
2022/04/12 PostgreSQL
HTML静态页面获取url参数和UserAgent的实现
2022/08/05 HTML / CSS