python中文件的相对路径

2019-07-11  本文已影响0人  Sissilovely

在编写的py文件中打开文件的时候经常见到下面其中路径的表达方式:

一个.代表当前绝对路径

两个.代表当前文件夹的上一级的绝对路径

则如下图从read_file.py打开 report/html_report中文件,路径表示如下

report_dir='../../report/html_report/'

我需要再report/html_report中创建一个文件,代码如下:

import time

report_dir='../../report/html_report/'

now=time.strftime('%Y-%m-%d %H_%M_%S')

report_name=report_dir+ now +'_result.html'

file=open(report_name,'w+')

file.write(now)
image
上一篇 下一篇

猜你喜欢

热点阅读