简体   繁体   English

Python os.remove(filename) 抛出错误“已被 temp 使用”

[英]Python os.remove(filename) throws error “already being used by temp”

My current code is我目前的代码是

import os
import pickle
import tempfile

# Pickle on HIGHEST_PROTOCOL breaks on Python 3.6.5
_PICKLE_PROTOCOL = 4
#CHANGED FROM 2 TO 4 TO ACCOMODATE VERSION, DONE BY USER


def _pickle_iterable(filename, iterable):
    with open(filename, 'wb') as pickle_fh:
        pklr = pickle.Pickler(pickle_fh, _PICKLE_PROTOCOL)
        for entry in iterable:
            pklr.dump(entry)
            pklr.clear_memo()


def _open_pickle(filename):
    return open(filename, 'rb')


def _unpickle_iterable(pickle_fh):
    with pickle_fh:
        unpklr = pickle.Unpickler(pickle_fh)
        try:
            while True:
                yield unpklr.load()
        except EOFError:
            pass


def file_buffered_tee(iterable, n=2):
    _,  filename = tempfile.mkstemp()
    try:
        _pickle_iterable(filename, iterable)
        return tuple(_unpickle_iterable(_open_pickle(filename)) for _ in range(n))
    finally:
        os.remove(filename)

os.remove(filename) gives the error os.remove(filename) 给出错误

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\s%username%\\AppData\\Local\\Temp\\tmpaoos53ie'

I don't know how to fix it, and the GitHub repo I pulled this from is archived, and I can't open another issue request.我不知道如何修复它,我从中提取的 GitHub 存储库已存档,我无法打开另一个问题请求。

I'm coming back to stackoverflow, and everywhere else I've seen I cannot find an applicable answer, and while I think I understand the code, I keep hitting errors lol我要回到stackoverflow,在我见过的其他任何地方我都找不到适用的答案,虽然我认为我理解了代码,但我一直遇到错误哈哈

Any help is appreciated!任何帮助表示赞赏! Thank you!谢谢!

edit 1: forgot imports... dumb mistake编辑1:忘记进口......愚蠢的错误

edit 2: I left out code because I thought it was un-needed.编辑 2:我遗漏了代码,因为我认为它是不需要的。 Thank you all for being patient:P谢谢大家耐心等待:P

edit 3: Traceback:编辑3:回溯:

line37, in file_buffered_tee:

os.remove(filename)

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\%username%\\AppData\\Local\\Temp\\tmp*xxxxxxx*'

edit 4: apparently the same issue was being had here , however it does everything the answer says to do, but returns the same error... Still not answered here and still very confused.编辑4:显然这里有同样的问题,但是它完成了答案所说的一切,但返回相同的错误......这里仍然没有回答,仍然很困惑。 Documentation hasnt helped either文档也没有帮助

You can tell tempfile to delete file upon close.您可以告诉 tempfile 在关闭时删除文件。 This way, when you close the file, the file will be deleted automatically.这样,当您关闭文件时,该文件将被自动删除。

This may help you: https://docs.python.org/3/library/tempfile.html这可能会对您有所帮助: https://docs.python.org/3/library/tempfile.html

tempfile.NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True , *, errors=None) tempfile.NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True , *, errors=None)

delete=True parameter is what you're looking for. delete=True 参数是您正在寻找的。 Now use tempfile to close the file via file.close()现在使用 tempfile 通过 file.close() 关闭文件

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

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