python 2 [错误32]进程无法访问该文件,因为它正被另一个进程使用

时间:2016-10-19 12:05:50

标签: python windows python-2.7 loops shutil

我正在使用python 2并阅读了几篇有关此错误的帖子,即(this post)。 但是,我仍然收到错误。 我所做的是: 我读取目录中的文件,如果任何文件包含特定的字符串,我删除该目录。

def select_poo():
path = os.walk('/paila_candonga/')
texto = 'poo'
extension = '.tex'
for root, dirs, files in path:
    for documento in files:
        if extension in documento:
            with open(os.path.join(root, documento), 'r') as fin:
                for lines in fin:
                    if texto in lines:
                        shutil.rmtree(root)
                    else:
                        continue

然后我收到错误:

WindowsError: [Error 32] The process cannot access the file because it is being used by another process

我也尝试过使用绝对路径:

def select_poo():
path = os.walk('/paila_candonga/')
texto = 'poo'
extension = '.tex'
for root, dirs, files in path:
    for documento in files:
        if extension in documento:
            with open(os.path.join(root, documento), 'r') as fin:
                for lines in fin:
                    if texto in lines:
                        route = (os.path.join(root, documento))
                        files = os.path.basename(route)
                        folder = os.path.dirname(route)
                        absolut= os.path.dirname(os.path.abspath(route))
                        todo = os.path.join(absolut, files)
                        print todo

                    else:
                        continue

然后我会得到:

C:\paila_candonga\la_Arepa.tex
C:\paila_candonga\sejodio\laOlla.tex
C:\paila_candonga\sejodio\laPaila.tex

如果我一次删除一个文件,使用相同的绝对路径和os.remove(''),我就不会遇到问题。如果我尝试使用select_poo()和shutil.rmtree(文件夹)或os.remove(绝对)一次删除所有文件,我将得到错误32.

有没有办法可以循环遍历todo中的每个路径并删除它们而不会出现错误32?

谢谢,

1 个答案:

答案 0 :(得分:2)

它发生在这里:

func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
    let containerView = transitionContext.containerView
    if transitionMode == .present {
        if let presentedView = transitionContext.view(forKey: UITransitionContextViewKey.to) {

            var viewCenter = presentedView.center
            var viewSize = presentedView.frame.size


            if UIDevice.current.userInterfaceIdiom == .pad {
                viewCenter = CGPoint(x: viewCenter.x, y: viewSize.height)
                viewSize = CGSize(width: viewSize.width, height: viewSize.height)
            }

            circle = UIView()
            circle.frame = frameForCircle(withViewCenter: viewCenter, size: viewSize, startPoint: startingPoint)
            circle.layer.cornerRadius = circle.frame.size.width / 2
            circle.center = startingPoint
            circle.backgroundColor = circleColor
            circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            circle.layer.masksToBounds = true
            containerView.addSubview(circle)

            presentedView.center = startingPoint
            presentedView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
            presentedView.alpha = 0
            containerView.addSubview(presentedView)

            UIView.animateKeyframes(withDuration: duration, delay: 0, options: .calculationModeLinear, animations: { 
                UIView.addKeyframe(withRelativeStartTime: 0, relativeDuration: 1, animations: { 
                    self.circle.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
                    presentedView.alpha = 1
                })
                UIView.addKeyframe(withRelativeStartTime: 0.19, relativeDuration: 1, animations: {
                    presentedView.transform = CGAffineTransform(scaleX: 1, y: 1)
                    presentedView.frame = CGRect(x: 0, y: (containerView.frame.size.height / 2)+10, width: containerView.frame.size.width, height: containerView.frame.size.height*0.5)
                })
                }, completion: { (sucess) in
                    transitionContext.completeTransition(sucess)
            })
        }
    } else {
        if let returningView = transitionContext.view(forKey: UITransitionContextViewKey.from) {
            let viewCenter = returningView.center
            let viewSize = returningView.frame.size

            circle.frame = frameForCircle(withViewCenter: viewCenter, size: viewSize, startPoint: startingPoint)
            circle.layer.cornerRadius = circle.frame.size.width / 2
            circle.center = startingPoint

            UIView.animate(withDuration: duration + 0.1, animations: {
                self.circle.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
                returningView.transform = CGAffineTransform(scaleX: 0.001, y: 0.001)
                returningView.center = self.startingPoint
                returningView.alpha = 0
                }, completion: {(success: Bool) in
                    returningView.center = viewCenter
                    returningView.removeFromSuperview()
                    self.circle.removeFromSuperview()
                    transitionContext.completeTransition(success)
            })
        }
    }
}

因此,您打开并锁定了文件,这就是您无法使用以下命令删除此文件夹的原因:

with open(os.path.join(root, documento), 'r') as fin:

在此声明中,您必须在shutil.rmtree(root) 声明

之外进行