Python知识点记录

1.路径相关

1.1 获取目录

import os
Path = os.getcwd()  #尽量用os.path.dirname(__file__)代替os.getcwd()去获取当前文件所在目录,避免以后被其他层级文件调用时出现问题
print('本文件所在目录:'+Path)
print('所在目录的上级目录'+os.path.dirname(Path))

1.2 获取当前系统下的路径间隔符(用于路径拼接) os.sep

import os 
path = os.getcwd()+os.sep+'新建文件'
#'C:\\Users\\ASUS\\新建文件'

2.时间相关

import time
>>> time.strftime('%Y%m%d')
'20200623'
>>> time.strftime('%Y-%m%d')
'2020-0623'
>>> time.strftime('%Y-%m-%d')
'2020-06-23'
>>> time.strftime('%Y-%m-%d %H:%M:%S')
'2020-06-23 20:44:55'

3.字符串处理

#将列表中的符号去掉并替换为自定义的符号
l = ['a','b','c']
s = '-'
s.join(l)
#'a-b-c'
posted @ 2020-06-23 20:41  秋夜花开  阅读(121)  评论(0编辑  收藏  举报