文件路径

plot.py文件路径:/Users/yangfengjuan/jupyter/plot.py

获取plot.py文件的绝对路径

1 currentpath = os.path.dirname(os.path.realpath(__file__)) 
2 运行结果:/Users/yangfengjuan/jupyter

获取当前执行文件的路径

1 currentpath = os.getcwd()
2 运行结果:/Users/yangfengjuan/jupyter

判断某个路径下是否存在某个文件

 1 os.path.exists()就是判断括号内的文件是否存在的意思,括号内可以是文件的路径
 2 os.path.exists('plot.py')
 3 运行结果:True

创建目录

在当前文件的同级文件夹plot下生成文件夹

 1 def get_content():
 2     temp_time = '2021-02-01'
 3     end_time = '2021-02-22'
 4     # 获取当前文件的绝对路径
 5     currpath = os.path.dirname(os.path.realpath(__file__))
 6     # 进入当前文件同级目录下的plot文件夹
 7     resultpath = currpath + os.sep + 'plot' + os.sep
 8     # plot文件夹下创建文件 2021-02-21_2021-02-28
 9     path = resultpath + str(temp_time).replace(' 00:00:00', '') + '~' + str(end_time).replace(' 00:00:00', '')
10 
11     # 判断plot文件夹下是否存在 拼接的文件夹
12     if not os.path.exists(path):
13         # 在plot文件夹下新建 拼接的文件夹
14         os.mkdir(path)
15     return path
16 get_content()

 

posted @ 2021-03-04 15:08  琥珀主yang  阅读(240)  评论(0编辑  收藏  举报