繁体   English   中英

PermissionError:[WinError 32]进程无法访问该文件,因为它正由另一个进程使用:

[英]PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:

我正在尝试测试我自己的反网络版本,可以在这里找到。 但是,我正在使用Pythons unittest模块进行测试。 这是代码:

import unittest
from unittest.mock import patch
from antiweb import main
import sys
import os
import tempfile
import shutil

class Test_Antiweb(unittest.TestCase):


def setUp(self):

    self.test_dir = tempfile.mkdtemp()
    self.testfile_source ="#@start()\n\n#@include(test_area)\n\n#@start(test_area)\n#This is test_area text\n#@(test_area)"
    with open(os.path.join(self.test_dir, "testfile.py"), "w") as test:
        test.write(self.testfile_source)

def test_antiweb(self):
    self.test_args = ['antiweb.py', '-i', "-o docs", os.path.join(self.test_dir, "testfile.py")]
    with patch.object(sys, 'argv', self.test_args):
        success = main()
    self.assertTrue(success)

def tearDown(self):

    shutil.rmtree(self.test_dir)



if __name__ == '__main__':
    unittest.main()

除了tearDown函数之外,一切正常。 在没有tearDown的情况下执行unittest时,temp文件夹和他的内容被完美地创建。 但是使用tearDown函数我得到一个错误:

======================================================================
ERROR: test_antiweb (antiweb_tests.Test_Antiweb)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Users\<username>\Documents\GitHub\antiweb\antiweb_tests.py", line 29,      in tearDown
shutil.rmtree(self.test_dir)
File "C:\Python34\lib\shutil.py", line 478, in rmtree
return _rmtree_unsafe(path, onerror)
File "C:\Python34\lib\shutil.py", line 377, in _rmtree_unsafe
onerror(os.rmdir, path, sys.exc_info())
File "C:\Python34\lib\shutil.py", line 375, in _rmtree_unsafe
os.rmdir(path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\
<username>\\AppData\\Local\\Temp\\tmp3lk01fn5'

当我查看temp文件夹时,文件夹本身仍然存在,但现在是空的。 在这里包含我的反网络文件太多了,所以如果你需要的话,我可以在这里再次链接它。

刚发生在我自己身上。 您的问题出在您的设置中

self.test_dir = tempfile.mkdtemp()返回文件描述符和路径的元组。 您需要在删除之前关闭文件描述符。

def setUp(self):

    self.fd, self.test_dir = tempfile.mkdtemp()
...
def tearDown(self):
    os.close(self.fd)
    shutil.rmtree(self.test_dir)

有关更详细的说明,请参阅此文章

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM