简体   繁体   English

Python不会关闭最后一个文件

[英]Python doesn't close the last file

I created this code to get all excel files in a folder and make a csv file to every sheet in every file.我创建了这段代码来获取一个文件夹中的所有 excel 文件,并为每个文件中的每个工作表制作一个 csv 文件。 This script works fine, but sometimes the last Excel file converted still locked by python on file system.此脚本工作正常,但有时最后转换的 Excel 文件仍被文件系统上的 python 锁定。 Can anyone help me to understand what's happening?谁能帮我理解发生了什么?

import sys
from os import listdir
from os.path import isfile, join
import pandas as pd
import csv
import re

def removeEspecialCharacters(obj):
    if isinstance(obj, str) :
        retorno = re.sub('[(\x90|\x8F)]','',obj).replace("\r","").replace("\n","")
    else:
        retorno = obj
        
    return retorno

myFolder = r'C:\Users\myuser\Downloads\ConvertFilesToCsv'
myFiles = [f for f in listdir(myFolder) if isfile(join(myFolder, f))]

for x in range(len(myFiles)):   
    if (myFiles[x].lower().endswith('.xls') or myFiles[x].lower().endswith('.xlsx') or myFiles[x].lower().endswith('.xlsb')):
        print('Converting file: '+myFiles[x]);
        if (myFiles[x].lower().endswith('.xlsb')):
            file = pd.ExcelFile(myFolder+'\\'+myFiles[x], engine='pyxlsb')
        else:
            file = pd.ExcelFile(myFolder+'\\'+myFiles[x])
    
        for mySheetName in file.sheet_names:
            df = pd.read_excel(file, sheet_name=mySheetName)
            df = df.applymap(removeEspecialCharacters)
            csvFileName = myFolder+'\\'+myFiles[x].replace('.xlsx','').replace('.xlsb','').replace('.xls','')+'_'+mySheetName+'.csv'
            df.to_csv(csvFileName,encoding='utf-8-sig',index=False,sep=",",quoting=csv.QUOTE_NONNUMERIC,quotechar="\"",escapechar="\"",decimal=".",date_format='%Y-%m-%d')#,quotechar='\'', escapechar='\\')
        file.close()
        file = ''

Note : this is a comment putting here for code format.注意:这是对代码格式的注释。

Your code looks fine to me.你的代码对我来说看起来不错。 I would advise you to use context management, similar to the doc , like this:我建议您使用类似于doc 的上下文管理,如下所示:

for filename in myFiles:   
    extension = filename.split('.')[-1]

    # you didn't seem to check xlsb in your code
    if extension not in ['xls', 'xlsx', 'xlsb']: 
        continue

    kwargs = {'engine': 'pyxlsb'} if extension=='xlsb' else {}

    with pd.ExcelFile(myFolder + '\\' + filename, **kwargs) as file:
        # do other stuff with file
        ...
        # you don't need to close file here
        # file.close()

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

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