我正在编写一些代码,根据文件的标题添加文件扩展名。使用任何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'
答案 0 :(得分:6)
在尝试重命名之前,您应关闭inputFile
:
...
inputFile = open((x), "rb")
byte1 = inputFile.read(1)
byte2 = inputFile.read(1)
inputFile.close()