如何解决如何删除附加到 Python 中的变量的文件
作为此代码的快速摘要,此脚本将 .xlsx 文件转换为 .pdf 文件。之后,我打算删除 excel 文件以确保我可以篡改 excel 文件(我尚未将 .pdf 文件设为只读)。
import pandas as pd
import pdfkit
from datetime import datetime,date,time
import time
import os
os = 'Windows 10'
file_name = 'report_prototype_excel1.xlsx'
company_name = 'ABC Company'
audit_frequency = 'ANNUAL'
file_time = time.strftime("%d%m%Y-%H%M%S")
if os == 'Windows 10':
config = pdfkit.configuration(wkhtmltopdf='C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe')
elif os == "Windows Server 2019":
print("Placeholder Windows Server 2019")
elif os == "MacOS":
print("Placeholder MacOS")
else:
print("Placeholder Ubuntu/Debian")
df = pd.read_excel(file_name)
df = df.fillna(" ")
df.to_html("report_prototype_html1.html")
options = {
'page-size': 'A4','margin-top': '0.75in','margin-right': '0.75in','margin-bottom': '0.75in','margin-left': '0.75in',}
pdfkit.from_file("report_prototype_html1.html",company_name + '_' + audit_frequency + '_audit_report_pdf_' + file_time + '.pdf',configuration=config,options=options)
os.remove (file_name)
但是,由于我要将文件分配给固定变量,“os.remove”命令不起作用,导致以下错误:
Traceback (most recent call last):
File "c:\Users\********\Documents\********\********\report_prototype_python_MAIN.py",line 33,in <module>
os.remove (file_name)
AttributeError: 'str' object has no attribute 'remove'
有没有办法解决我的困境?
解决方法
您已导入 os
模块并分配了一个变量 os = 'Windows 10'
。因为,os
现在是一个字符串,它没有您正在寻找的 remove
方法。
请将 os
变量重命名为其他名称。
os = 'Windows 10'
通过这句话覆盖了模块os
。
只需重命名您的变量。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。