Python日志记录模块,日志文件问题:PermissionError:[WinError 32]该进程无法访问该文件,因为该文件正在被另一个进程使用

edo101

我以前从未使用过日志记录,并且是Python的新手。我的指导者要求脚本必须包含一个日志文件。因此,我尝试按照他的模板在代码中执行此操作。下面的代码是我的代码中使用日志记录的摘录:

logfilepath = r"C:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\odfshistory\CSV_ODFS_Probe_Quality-%Y-%m-%d.log"

log_file_name = datetime.now().strftime(logfilepath)
print(log_file_name)

logging.basicConfig(
    filename=log_file_name,
    level=logging.DEBUG,
    format='[Probe Data Quality] %(asctime)s - %(name)s %(levelname)-7.7s %(message)s'    #can you explain this Tenzin?
)

def process_dirconfig_file(config_file_from_sysarg):
    logging.info('started processing config file' + (config_file_from_sysarg) )
    dirconfig_file_Pobj = Path(config_file_from_sysarg)
    try:
        if Path.is_file(dirconfig_file_Pobj):
            try:
                if ("db_string" not in parseddict or parseddict["db_string"] == ""):
                    raise Exception(f"Error: Your config file is missing 'db connection string' for db connection. Please edit config file to section[db string] and db string key val pairs of form: db_string = <db string>")
                    #print(f"Error: Your config file is missing 'error directory' for file processing")

            except Exception as e:
                raise Exception(e)  #logging.exception(e) puts it into log file
                logging.exception(e)
            return parseddict
        else:
            raise Exception(f"Error: No directory config file. Please create a config file of directories to be used in processing")
    except Exception as e:
        raise Exception(e)
        logging.exception(e)

if __name__ == '__main__':
    try:
        startTime = datetime.now()
        parse_dict = process_dirconfig_file(dirconfig_file)
        db_instance = dbhandler(parse_dict["db_string"])
        odfs_tabletest_dict = db_instance['odfs_tester_history_files']
        odf_history_from_csv_to_dbtable(db_instance)
        print(datetime.now() - startTime)
    except Exception  as e:
        logging.exception(e)

为什么这种结构会给我这个错误?当然,为了简洁起见,我的大多数代码都经过编辑。该代码有效。日志记录正在打破它。我还将注意到,我的许多函数都用于for循环,但我认为这不会成为问题。并非日志文件无法在for循环中工作。我还没有做任何线程化。

还有如何使它为每次运行创建一个新的日志文件。现在,日志记录信息会附加到日志文件吗?

错误:

[Probe Data Quality] 2020-07-02 09:21:04,217 - root INFO    started processing config fileC:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\venv\odfs_tester_history_dirs.ini
[Probe Data Quality] 2020-07-02 09:21:04,373 - root INFO    started processing config fileC:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\venv\odfs_tester_history_dirs.ini
[Probe Data Quality] 2020-07-02 09:21:04,420 - root ERROR   [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\sys_nsgprobeingestio\\Documents\\dozie\\odfs\\odfshistory\\CSV_ODFS_Probe_Quality-2020-07-02-09-21-04.log' -> 'C:\\Users\\sys_nsgprobeingestio\\Documents\\dozie\\odfs\\odfshistory\\error\\CSV_ODFS_Probe_Quality-2020-07-02-09-21-04.log'
Traceback (most recent call last):
  File "C:/Users/sys_nsgprobeingestio/Documents/dozie/odfs/odfshistory3.py", line 277, in <module>
    odf_history_from_csv_to_dbtable(db_instance)
  File "C:/Users/sys_nsgprobeingestio/Documents/dozie/odfs/odfshistory3.py", line 251, in odf_history_from_csv_to_dbtable
    csv.rename(trg_path.joinpath(csv.name))
  File "C:\ProgramData\Anaconda3\lib\pathlib.py", line 1329, in rename
    self._accessor.rename(self, target)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\sys_nsgprobeingestio\\Documents\\dozie\\odfs\\odfshistory\\CSV_ODFS_Probe_Quality-2020-07-02-09-21-04.log' -> 'C:\\Users\\sys_nsgprobeingestio\\Documents\\dozie\\odfs\\odfshistory\\error\\CSV_ODFS_Probe_Quality-2020-07-02-09-21-04.log'
蒙施

看起来odf_history_from_csv_to_dbtable是在记录器仍在使用时尝试重命名文件吗?可能需要将该逻辑移到其他地方,或者重新考虑在运行主脚本后将要执行的操作。self._accessor.rename(self, target)并似乎正在尝试将其移动到error目录?

至于每次运行脚本都有唯一的名称:

from uuid import uuid4

run_id = uuid4()
logfilepath = f"C:\Users\sys_nsgprobeingestio\Documents\dozie\odfs\odfshistory\CSV_ODFS_Probe_Quality-%Y-%m-%d-{run_id}.log"

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

Related 相关文章

热门标签

归档