import os, sys
# 获取当前脚本所在路径
scriptRunPath = os.path.abspath(__file__)
print(scriptRunPath)
# 获取当前脚本的上级目录
dirScriptRunPath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(dirScriptRunPath)
# 路径拼接
path = os.path.join(dirScriptRunPath, 'script')
print(path)
# 把路径分为目录和文件两个部分,以列表返回。
path = os.path.split(os.path.abspath(__file__))
# 添加新路径到环境变量
newPath = sys.path.append(path)
newPath = sys.path.insert(0, path)
print(newPath)
# 获取当前工作目录
cur_path = os.getcwd()
# 切换工作目录
os.chdir(dirpath)
# Windows路径分隔符
os.path.sep
# Linux路径分隔符
os.path.altsep
pthon提示-bash: ./monitor.py: /usr/bin/python^M: bad interpreter: No such file or directory错误解决方法
1.查看文件是dos格式还是unix格式:
vi monitor.py
:set ff
2.如果是dos格式,则将文件转换为unix格式
:set ff=unix
:wq
import os, sys, time, datetime
def line_file_test():
print(time.strftime("%Y-%m-%d %H:%M:%S")) # 获取当前时间
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) # 获取当前时间
print(datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')) # 获取当前UTC时间
print(sys._getframe().f_lineno) # 获取当前行号(行数)
print(os.path.abspath(__file__)) # 获取当前Python文件的完整路径
print(os.path.basename(__file__)) # 获取当前Python文件文件名
print(sys._getframe().f_code.co_name) # 在函数内部获取当前函数名
if __name__ == '__main__':
line_file_test()
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# UTC时间与北京时间相互转换
import datetime, time
# Windows UTC时间戳
timestamp = 132979302739322498
# 将Windows UTC时间戳转换为Linux UTC时间字符串
value = datetime.datetime(1601, 1, 1) + datetime.timedelta(seconds=timestamp / 10000000)
print(value)
# 格式化UTC时间为标准的UTC时间格式
UTC_FORMAT = value.strftime('%Y-%m-%dT%H:%M:%SZ')
print(UTC_FORMAT)
# 格式化时间字符串
UTCTIME = value.strftime('%Y-%m-%d %H:%M:%S')
print(UTCTIME)
# 将时间字符串转换为时间数组
UTC_TIME = time.strptime(UTCTIME, '%Y-%m-%d %H:%M:%S')
print(UTC_TIME)
# 将时间数组转换为时间戳
utc_time = time.mktime(UTC_TIME)
# 将UTC时间戳转换为东八区的时间戳
beijing_time = utc_time + 8 * 60 * 60
print(beijing_time)
# 将时间戳转换为localtime然后格式化
LOCALTIME = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(beijing_time))
print(LOCALTIME)
# 将北京时间转换为UTC时间
local_time = time.strftime("%Y-%m-%d %H:%M:%S")
print(local_time)
print(time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime(time.mktime(time.strptime(local_time, '%Y-%m-%d %H:%M:%S')) - 8*60*60)))