python創(chuàng)建和刪除目錄的方法
更新時間:2015年04月29日 09:51:07 作者:重負在身
這篇文章主要介紹了python創(chuàng)建和刪除目錄的方法,涉及Python操作目錄的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下
本文實例講述了python創(chuàng)建和刪除目錄的方法。分享給大家供大家參考。具體分析如下:
下面的代碼可以先創(chuàng)建一個目錄,然后調(diào)用自定義的deleteDir函數(shù)刪除整個目錄
#-------------------------------------- # 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程序設(shè)計有所幫助。
您可能感興趣的文章:
相關(guān)文章
Python使用matplotlib的pie函數(shù)繪制餅狀圖功能示例
這篇文章主要介紹了Python使用matplotlib的pie函數(shù)繪制餅狀圖功能,結(jié)合實例形式分析了Python使用matplotlib的pie函數(shù)進行餅狀圖繪制的具體操作技巧,注釋中對pie函數(shù)的用法進行了詳細的說明,便于理解,需要的朋友可以參考下2018-01-01tensorflow 獲取checkpoint中的變量列表實例
今天小編就為大家分享一篇tensorflow 獲取checkpoint中的變量列表實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實現(xiàn)方法
這篇文章主要介紹了Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09