python创建和删除文件有什么区别_python创建与删除临时文件夹的例子

本节主要内容:

python创建与删除临时文件夹。

1,python创建文件夹 代码示例:

import tempfile, os

tempfd, tempname = tempfile.mkstemp('.suffix')

os.write(tempfd, "aString") # or, if you want a file-object: os.fdopen(tempfd, 'w+')

os.close(tempfd)

os.unlink(tempname)

2,Python 删除指定文件夹中的文件 代码示例:

import win32con, win32api,shutil,os

def removePath(destinationPath):

'''

@summary: 删掉destinationPath目录,当然包括其中的子目录和文件

@param destinationPath: 所给目标目录

'''

if os.path.exists(destinationPath):

pathList = os.listdir(destinationPath)

for path in pathList: # www.#

pathFull = os.path.join(destinationPath,path)

if os.path.isfile(pathFull):

win32api.SetFileAttributes(pathFull, win32con.FILE_ATTRIBUTE_NORMAL)#to be able to delete the file we need to set it back to normal:

if os.path.isdir(pathFull):#if pathful is a dir, then call removePath function

removePath(pathFull)

shutil.rmtree(destinationPath,True)#use rmtree to remove the whole path tree

if __name__ == '__main__':

path = r'C:\Users\Administrator\AppData\Local\Temp'#删除临时文件夹中的文件

removePath(path)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值