时间,路径,文件,异常,编码

一、时间

import time

print('获取当前时间<str类型>: ', time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())  )
#获取当前时间<str类型>:                    2019-03-14 21:53:28
print('获取当前时间戳<float类型>: ', time.time()  )
#获取当前时间戳<float类型>:                1552571608.9545915

from datetime import datetime,date

print("今天日期<class 'datetime.date'>:", date.today()  )
#今天日期<class 'datetime.date'>:          2019-03-14
print("今天日期+时间<class 'datetime.datetime'>:", datetime.today()  )
#今天日期+时间<class 'datetime.datetime'>: 2019-03-14 21:53:28.958581
print("当前时间<class 'datetime.datetime'>:", datetime.now()  )
#当前时间<class 'datetime.datetime'>:      2019-03-14 21:53:28.958580
import datetime
start = datetime.datetime.now()
end = (start + datetime.timedelta(days=+30)).strftime("%Y-%m-%d")  
start = start.strftime("%Y-%m-%d")

print(start,end)

二、路径、文件

其他用法参考:https://www.cnblogs.com/kaid/p/9252084.html

import os.path
a=os.getcwd()               ;print('当前路径:',a)
b=os.path.dirname(a)        ;print('上级路径:',b)
c=os.path.abspath('../..')  ;print('上上级路径:',c)

f=os.path.abspath(__file__) ;print('文件路径:',f)
f1=os.path.split(f)         ;print('文件名:',f1[1])

pj='\'.join([a,'pj文件夹','pj文件.txt'])    ;print('拼接路径:',pj)
#如果不存在目录就创建(不能创建文件)
if os.path.exists(a+'\pj文件夹')==False:
    os.mkdir(a+'\pj文件夹')
#如果不存在文件就创建
with open(pj,'w') as file:
    file.write('文件写入')

'''
'r':读,若不存在会报错
'w':写,若不存在就创建
'a':追加写入,若不存在就创建
'r+' == r+w(可读可写,文件若不存在就报错(IOError))
'w+' == w+r(可读可写,文件若不存在就创建)
'a+' ==a+r(可追加可写,文件若不存在就创建)
对应的,如果是二进制文件,就都加一个b就好啦:
'rb'  'wb'  'ab'  'rb+'  'wb+'  'ab+'

file.read(N)  读取N比特数据
file.readline()  读取第一行数据
file.readlines()  读取所有行
file.write('内容')  写入内容
file.close()  关闭文件
''' import sys print('默认搜索路径:',sys.path) sys.path.append(r'H:PycharmProjects接口unittest_t3') print('添加后的搜索路径:',sys.path)
# 自己写的一个获取根目录方法
def root_path(): p=os.getcwd() while True: p=os.path.dirname(p) if os.path.split(p)[1]=='目标目录': break return p

  

三、异常

这里就写了一个常用的异常处理,其他异常参考:http://www.runoob.com/python/python-exceptions.html

try:
    int('a')
except Exception as e:
    print('异常信息:',e)

 四、编码

1.为了处理英文字符,产生了ASCII码;
2.为了处理中文字符,产生了GB2312;
3.为了处理各国字符,产生了Unicode;
4.为了提高Unicode存储和传输性能,产生了UTF-8,它是Unicode的一种实现形式。

'''
1.Python2中默认的字符编码是ASCII码。
2.Python2中字符串有str和unicode两种类型。str有各种编码的区别,unicode是没有编码的标准形式。
3.Python2中可以直接查看到unicode的字节串
'''

u = u'中文' #显示指定unicode类型对象u
str = u.encode('gb2312') #以gb2312编码对unicode对像进行编码
str1 = u.encode('gbk') #以gbk编码对unicode对像进行编码
str2 = u.encode('utf-8') #以utf-8编码对unicode对像进行编码
u1 = str.decode('gb2312')#以gb2312编码对字符串str进行解码,以获取unicode
u2 = str.decode('utf-8')#如果以utf-8的编码对str进行解码得到的结果,将无法还原原来的unicode类型
#文件读取问题
f = open('test.txt','r')
s = f.read() #读取文件内容,如果是不识别的encoding格式(识别的encoding类型跟使用的系统有关),这里将读取失败
'''假设文件保存时以gb2312编码保存'''
u = s.decode('gb2312') #以文件保存格式对内容进行解码,获得unicode字符串
'''下面我们就可以对内容进行各种编码的转换了'''
str = u.encode('utf-8')#转换为utf-8编码的字符串str
str1 = u.encode('gbk')#转换为gbk编码的字符串str1
str1 = u.encode('utf-16')#转换为utf-16编码的字符串str1

参考来源:

https://blog.csdn.net/wz947324/article/details/80625533

https://www.cnblogs.com/dongfengl/p/10093751.html

原文地址:https://www.cnblogs.com/yinwenbin/p/10533372.html