python删除文件代码_python2.7删除文件夹和删除文件代码实例

#!c:\python27\python.exe

# -*- coding: utf-8 -*-

import os

import re

from os import path

from shutil import rmtree

DEL_DIRS = None

DEL_FILES = r'(.+?\.pyc$|.+?\.pyo$|.+?\.log$)'

def del_dir(p):

"""Delete a directory."""

if path.isdir(p):

rmtree(p)

print('D : %s' % p)

def del_file(p):

"""Delete a file."""

if path.isfile(p):

os.remove(p)

print('F : %s' % p)

def gen_deletions(directory, del_dirs=DEL_DIRS, del_files=DEL_FILES):

"""Generate deletions."""

patt_dirs = None if del_dirs == None else re.compile(del_dirs)

patt_files = None if del_files == None else re.compile(del_files)

for root, dirs, files in os.walk(directory):

if patt_dirs:

for d in dirs:

if patt_dirs.match(d):

yield path.join(root, d)

if patt_files:

for f in files:

if patt_files.match(f):

yield path.join(root, f)

def confirm_deletions(directory):

import Tkinter

import tkMessageBox

root = Tkinter.Tk()

root.withdraw()

res = tkMessageBox.askokcancel("Confirm deletions?",

"Do you really wish to delete?\n\n"

"Working directory:\n%s\n\n"

"Delete conditions:\n(D)%s\n(F)%s"

% (directory, DEL_DIRS, DEL_FILES))

if res:

print('Processing...')

m, n = 0, 0

for p in gen_deletions(directory):

if path.isdir(p):

del_dir(p)

m += 1

elif path.isfile(p):

del_file(p)

n += 1

print('Clean %d dirs and %d files.' % (m, n))

root.destroy()

else:

print('Canceled.')

root.destroy()

root.mainloop()

if __name__ == '__main__':

import sys

argv = sys.argv

directory = argv[1] if len(argv) >= 2 else os.getcwd()

confirm_deletions(directory)

# import subprocess

# subprocess.call("pause", shell=True)

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值