python創(chuàng)建和刪除目錄的方法
本文實(shí)例講述了python創(chuàng)建和刪除目錄的方法。分享給大家供大家參考。具體分析如下:
下面的代碼可以先創(chuàng)建一個(gè)目錄,然后調(diào)用自定義的deleteDir函數(shù)刪除整個(gè)目錄
#--------------------------------------
# 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" );
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
pandas read_excel()和to_excel()函數(shù)解析
這篇文章主要介紹了pandas read_excel()和to_excel()函數(shù)解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Python采集數(shù)據(jù)保存CSV文件出現(xiàn)內(nèi)容亂碼的解決方法
這篇文章主要為大家詳細(xì)介紹了如何解決Python中保存CSV文件內(nèi)容亂碼的問題,并提供詳細(xì)的示例代碼以更好地理解和解決這個(gè)問題,希望對(duì)大家有所幫助2024-03-03
python實(shí)現(xiàn)關(guān)鍵詞提取的示例講解
下面小編就為大家分享一篇python實(shí)現(xiàn)關(guān)鍵詞提取的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python JSON格式數(shù)據(jù)的提取和保存的實(shí)現(xiàn)
這篇文章主要介紹了Python JSON格式數(shù)據(jù)的提取和保存的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
python數(shù)據(jù)結(jié)構(gòu)之鏈表的實(shí)例講解
下面小編就為大家?guī)硪黄猵ython數(shù)據(jù)結(jié)構(gòu)之鏈表的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
Python自動(dòng)化處理日常任務(wù)的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用Python自動(dòng)化處理日常任務(wù),例如自動(dòng)化文件管理,自動(dòng)化定時(shí)任務(wù),自動(dòng)化發(fā)送郵件等,有需要的小伙伴可以參考一下2025-01-01

