OSError:从CSV文件读取数据时,从文件初始化失败

时间:2019-10-17 08:33:19

标签: python python-3.x pandas openpyxl

我知道那里有帖子提到我面临的相同问题,但是这里的情况有所不同。

有时,当我尝试读取CSV时,它会被顺利读取,但是有时候,当我尝试使用pandas中的read_csv()函数读取相同的文件时,它向我显示了以下错误提示: File Showing Error!

现在以下屏幕截图显示了已成功读取同一文件:

enter image description here

我无法找出错误的解决方案。

这是我的示例Excel文件的屏幕截图。 enter image description here

读取数据框后,在CSV文件上执行“ 打印(df.dtypes)”后的

屏幕截图。 enter image description here

Python代码: Append_function.py

import pandas as pd
import openpyxl as ox


def append_df_to_excel(filename, df, sheet_name='Sheet1', start_row=None,
                   truncate_sheet=False,
                   **to_excel_kwargs):
# ignore [engine] parameter if it was passed

if 'engine' in to_excel_kwargs:

    to_excel_kwargs.pop('engine')

writer = pd.ExcelWriter(filename, engine='openpyxl')

# Python 2.x: define [FileNotFoundError] exception if it doesn't exist
# try:
#     FileNotFoundError
# except NameError:
#     FileNotFoundError = IOError

try:
    # try to open an existing workbook
    writer.book = ox.load_workbook(filename)

    # get the last row in the existing Excel sheet
    # if it was not specified explicitly
    if start_row is None and sheet_name in writer.book.sheetnames:
        start_row = writer.book[sheet_name].max_row

    # truncate sheet
    if truncate_sheet and sheet_name in writer.book.sheetnames:
        # index of [sheet_name] sheet
        idx = writer.book.sheetnames.index(sheet_name)
        # remove [sheet_name]
        writer.book.remove(writer.book.worksheets[idx])
        # create an empty sheet [sheet_name] using old index
        writer.book.create_sheet(sheet_name, idx)

    # copy existing sheets
    writer.sheets = {ws.title: ws for ws in writer.book.worksheets}
except FileNotFoundError:
    # file does not exist yet, we will create it
    print("File does not exist")
    pass

if start_row is None:
    start_row = 0

# write out the new sheet
df.to_excel(writer, sheet_name, startrow=start_row, **to_excel_kwargs, header=True, float_format="%.2f")

print(df.dtypes)

# save the workbook
writer.save()

Main_file.py

import os
import pandas as pd
from PyQt5 import QtCore, QtWidgets
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
import time
from Append_Function import append_df_to_excel



class Emitter(QtCore.QObject):
    newDataFrameSignal = QtCore.pyqtSignal(pd.DataFrame)


class Watcher:
    def __init__(self):
    self.watch_dir = os.getcwd()
    self.directory_to_watch = None
    self.emitter = Emitter()
    self.observer = Observer()
    self.event_handler = Handler(
        emitter=self.emitter,
        patterns=["*.CSV"],
        ignore_patterns=["*.tmp"],
        ignore_directories=True
    )

def set_filename(self, filename):
    self.directory_to_watch = os.path.join(self.watch_dir, filename)

def run(self):
    self.observer.schedule(self.event_handler, self.directory_to_watch, recursive=False)
    self.observer.start()

def stop_watcher(self):
    self.observer.stop()


class Handler(PatternMatchingEventHandler):
def __init__(self, *args, emitter=None, **kwargs):
    super(Handler, self).__init__(*args, **kwargs)
    self._emitter = emitter
    self.file_name = time.strftime("%Y%m%d-%H%M%S")+".xlsx"


def on_any_event(self, event):

    if event.is_directory:
        return None
    elif event.event_type == 'created':
        # Take any action here when a file is first created.
        print("Received created event - %s." % event.src_path)

        if os.path.isfile(os.path.join(os.getcwd(), self.file_name)):

            append_df_to_excel(os.path.join(os.getcwd(), self.file_name),
                               pd.read_csv(event.src_path, header=1, index_col=0))
            df = pd.read_csv(event.src_path, header=1, engine='python',encoding="utf-8")

        else:
            append_df_to_excel(os.path.join(os.getcwd(), self.file_name),
                               pd.read_csv(event.src_path, header=0, index_col=0))
            df = pd.read_csv(event.src_path, header=0, engine='python',encoding="utf-8")

        self._emitter.newDataFrameSignal.emit(df.copy())
        df.set_index(df.columns.values.tolist()[0], inplace=True)

    elif event.event_type == 'modified':
        print("Modified created event - %s." % event.src_path)

        if os.path.isfile(os.path.join(os.getcwd(), self.file_name)):

            append_df_to_excel(os.path.join(os.getcwd(), self.file_name),
                               pd.read_csv(event.src_path, header=1, index_col=0))
            df = pd.read_csv(event.src_path, header=1, engine='python',encoding="utf-8")

        else:

            append_df_to_excel(os.path.join(os.getcwd(), self.file_name),
                               pd.read_csv(event.src_path, header=0, index_col=0),)
            df = pd.read_csv(event.src_path, header=0, engine='python',encoding="utf-8")

        self._emitter.newDataFrameSignal.emit(df.copy())
        df.set_index(df.columns.values.tolist()[0], inplace=True)


class DataFrameTableWidget(QtWidgets.QTableWidget):

@QtCore.pyqtSlot(pd.DataFrame)
def append_dataframe(self, df):
    df = df.copy()
    if df.columns.size > self.columnCount():
        self.setColumnCount(df.columns.size)
    r = self.rowCount()
    self.insertRow(r)
    for c, column in enumerate(df):
        it = QtWidgets.QTableWidgetItem(column)

        self.setItem(r, c, it)
    i = self.rowCount()
    for r, row in df.iterrows():
        self.insertRow(self.rowCount())
        for c, (column, value) in enumerate(row.iteritems()):
            it = QtWidgets.QTableWidgetItem(str(value))
            self.setItem(i+r, c, it)

任何建议将不胜感激。预先感谢!

1 个答案:

答案 0 :(得分:1)

尝试在循环时读取文件:

def on_created(event):

    ...

    # WAITING FILE TRANSFERING
    file = None
    while file is None:
        try:
            file = open(event.src_path)
        except OSError:
            file = None
            print("WAINTING FILE TRANSFERING....")
            time.sleep(3)