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ì)有所幫助。
- 如何基于Python創(chuàng)建目錄文件夾
- python 模擬創(chuàng)建seafile 目錄操作示例
- Python腳本按照當(dāng)前日期創(chuàng)建多級(jí)目錄
- 解決python os.mkdir創(chuàng)建目錄失敗的問題
- Python實(shí)現(xiàn)按當(dāng)前日期(年、月、日)創(chuàng)建多級(jí)目錄的方法
- 用Python刪除本地目錄下某一時(shí)間點(diǎn)之前創(chuàng)建的所有文件的實(shí)例
- python實(shí)現(xiàn)一次創(chuàng)建多級(jí)目錄的方法
- Python創(chuàng)建系統(tǒng)目錄的方法
- python判定文件目錄是否存在及創(chuàng)建多層目錄
相關(guān)文章
python微信公眾號(hào)開發(fā)簡(jiǎn)單流程實(shí)現(xiàn)
這篇文章主要介紹了python微信公眾號(hào)開發(fā)簡(jiǎn)單流程實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Python使用matplotlib的pie函數(shù)繪制餅狀圖功能示例
這篇文章主要介紹了Python使用matplotlib的pie函數(shù)繪制餅狀圖功能,結(jié)合實(shí)例形式分析了Python使用matplotlib的pie函數(shù)進(jìn)行餅狀圖繪制的具體操作技巧,注釋中對(duì)pie函數(shù)的用法進(jìn)行了詳細(xì)的說明,便于理解,需要的朋友可以參考下2018-01-01
tensorflow 獲取checkpoint中的變量列表實(shí)例
今天小編就為大家分享一篇tensorflow 獲取checkpoint中的變量列表實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法
這篇文章主要介紹了Pytorch之Tensor和Numpy之間的轉(zhuǎn)換的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

