python删除进程_Python删除“当前正被另一个进程使用”的文件

要求

我必须尝试创建一个程序,删除所有损坏的图像(以及小于400x400的图像),并将其余图像过滤成10000个组。在

问题

当前,当我尝试删除任何“损坏”的图像时,它表示该文件当前正被另一个进程使用,每个错误如下:The process cannot access the file because it is being used by another process.

采取的步骤

我尝试了多种方法来释放文件,包括使用“后退踏板”策略,即应用程序移动到下一个图像,然后后退踏板尝试删除该图像,但它仍然保持打开状态。

如果我尝试在Python打开时手动删除图像,它会很顺利地完成。在

请参见下面的代码:def confirmIt():

#======== Confirm Selection and Move files to new sub-directory:

if not folderPath.get() == "": ## make sure not blank

source = folderPath.get() ## set source path

size = 0

broken = False

for fname in os.listdir(source):

if fname.lower().endswith(extensions):

imageName = source+"\\"+fname ## set the source location of the image

try:

img = Image.open(imageName)

width, height = img.size ## get the dimensions

size = width * height / 1000

broken = False

img.close()

except IOError, e:

broken = True

img.close()

if ( broken == True ):

def handleRemoveReadonly(func, path, exc):

excvalue = exc[1]

if func in (os.rmdir, os.remove) and excvalue.errno == errno.EACCES:

os.chmod(path, stat.S_IRWXU| stat.S_IRWXG| stat.S_IRWXO)

func(path)

else:

raise

try:

os.remove(imageName) ## Remove all remaining images that don't match the preset requirements (<400 and is an image)

额外信息

请注意,我也在使用GUI,因此“resultMessage”和类似的输出/输入字段就是出于这个原因。在

编辑:

经过与@Cyphase的反复讨论,我已经确定了问题所在。落榜的原因是我对他进行了追踪编辑。我并不真正使用这个论坛,因为我通常不需要编写代码。此应用程序的其他主题可能会出现。谢谢。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值