【问题标题】:Error in using resolve() while naming output files命名输出文件时使用 resolve() 时出错
【发布时间】:2020-10-28 02:52:11
【问题描述】:
output_file_name = '{}__output1.xlsx'.format(str(in_file.resolve().stem))

output_file_path = str(Path(out_folder, output_file_name))

此代码行给出以下错误:

AttributeError: 'str' object has no attribute 'resolve'

我正在处理多个文件并尝试为输出文件动态创建文件名及其路径。需要您的帮助来解决错误。

我正在尝试通过以下方式保存文件:

df.style.applymap(color_code).\to_excel(output_file_path, engine="xlsxwriter")

【问题讨论】:

  • this。我的意思是特别是 Salo 的评论。

标签: python


【解决方案1】:

resolve() 是 Python 的 stdlib pathlib 模块中的 Path 对象上的一个方法。您应该可以通过将 str 转换为 Path 对象来修复它。

from pathlib import Path

output_file_name = '{}__output1.xlsx'.format(Path(in_file).resolve().stem)

output_file_path = str(Path(out_folder, output_file_name))

【讨论】:

  • 我确实按照您的建议更改了代码,但仍然出现相同的错误。
  • 我刚刚编辑了我的答案,注意括号的位置。
猜你喜欢
  • 2019-01-04
  • 2014-10-27
  • 2016-08-07
  • 1970-01-01
  • 2018-03-10
  • 2019-08-08
  • 2021-09-14
  • 2021-03-31
  • 2020-06-29
相关资源
最近更新 更多