【发布时间】:2019-01-13 12:42:21
【问题描述】:
我正在尝试创建一个函数来检查特定的 docx 文件是否存在,如果不存在则创建该文件。我如何设置它以便程序检查 .py 文件所在的文件。
#Creating The Finance Log Word Doc
#If the file does not exist create file
if os.path.exists("Finance Log.docx")==False:
doc = docx.Document()
run = doc.add_paragraph().add_run()
# Apply Style
Tstyle = doc.styles['Normal']
font = Tstyle.font
font.name = "Nunito Sans"
font.size = Pt(48)
Title = doc.add_paragraph()
TRun = Title.add_run("Finance Log")
TRun.bold = True
doc.add_picture('Scouts_Logo_Stack_Black.png', width=Inches(4.0))
doc.save("Finance Log.docx")
预期的结果是文件被创建,仅当与 .py 文件不在同一文件夹时。
实际结果是函数一直在执行,因为文件路径设置不正确。
【问题讨论】:
标签: python python-3.x filepath docx