我正在编写一些代码,根据文件的头向文件添加扩展名对于任何gzip文件,我都在提取数据。
当我尝试运行代码时,我得到一个winerror 32。下面是代码和错误
谢谢你的建议。

def extract():
    os.chdir("C:/Users/David/MyFiles")
    files = os.listdir(".")
    for x in (files):
        inputFile = open((x), "rb")
        byte1 = inputFile.read(1)
        byte2 = inputFile.read(1)
        if byte1 == b'\x1f' and byte2 == b'\x8b':
            os.rename((x), (x) + ".gz")
            file = gzip.open((x), "rb")
            content = file.read()
            with open((x), "wb") as outputFile:
                outputFile.write(content)

错误:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'f_000002.gz'

最佳答案

您应该先关闭inputFile,然后再尝试重命名它:

...
    inputFile = open((x), "rb")
    byte1 = inputFile.read(1)
    byte2 = inputFile.read(1)
    inputFile.close()

10-08 11:03