https://blog.csdn.net/jiede1/article/details/79064780
1.支持配置文件格式
[DEFAULT] ServerAliveInterval = 45 Compression = yes CompressionLevel = 9 ForwardX11 = yes [bitbucket.org] User = hg [topsecret.server.com] Port = 50022 ForwardX11 = no
首先是由一个sections(如 [DEFAULT] )引起,下面接类似“name = value”形式的key-value对。其实,接 “name : value”的形式也可以。
2.函数API
-read(filename) 直接读取文件内容
-sections() 得到所有的section,并以列表的形式返回
-options(section) 得到该section的所有option
-items(section) 得到该section的所有键值对
-get(section,option) 得到section中option的值,返回为string类型
-getint(section,option) 得到section中option的值,返回为int类型,还有相应的getboolean()和getfloat() 函数。
-add_section(section) 添加一个新的section
-set( section, option, value) 对section中的option进行设置
需要调用write将内容写入配置文件。
设备授权 防止拷贝
path='/proc/cpuinfo'
f = open(path, mode='r', encoding='utf-8')
lines = f.readlines() # 以行的形式进行读取文件
#list1 = []
for line in lines:
a=line.strip().split(':') # x.strip()#除去每行的换行符 按照:分割
b = a[0:1] # list--str
c = "".join(b).strip() # 去除空格
if c=='Serial':
b = a[1:2] # 这是选取需要读取的位数
c="".join(b).strip() # 去除空格
print(c)
#list1.append(c) # 将其添加在列表之中
if c=='000000002b670eb2' :
print('设备授权正确')
f.close()
#for i in list1:
# print(i)
2https://blog.csdn.net/liuchunming033/article/details/39376147
准备一个文件text.txt,内容为一行"AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD"。
''' Created on Sep 18, 2014 @author: liu.chunming ''' #There is a text.txt file which contains "AAAAAAAABBBBBBBBCCCCCCCCDDDDDDDD" text_file=r"C:\Users\liu.chunming\Desktop\test.txt" #open() f=open(text_file,"r") #seek() f.seek(8,0) #tell pos=f.tell() #read() text_to_number=f.read(8) f.seek(8,1) text_to_all=f.read() f.close() print pos print text_to_number
结果
8 BBBBBBBB DDDDDDDD
代码分析:
这段代码涉及到文件操作的几个方法。
1.open()方法
用来打开一个文件。这是对文件操作的第一步。open()方法的语法如下:open(name[, mode[,buffering]])。name参数是open方法的唯一强制参数,用来标识要打开的文件名。mode是文件打开的模式,通常有三种:r为读模式打开,w为写模式打开,a为追加模式打开。
2.seek()方法
用它设置当前文件读/写指针的偏移。seek()方法的语法如下:fileObject.seek(offset[, whence])。offset参数指明偏移量,第二个参数指出第一个参数偏移基准是哪里:0 表示移动到一个绝对位置 (从文件开始算起),1 表示移到一个相对位置 (从当前位置算起),还有 2 表示对于文件尾的一个相对位置。”
3.tell()方法
返回当前文件指针的位置。
4.read()方法
读取文件内容的方法。读取文件内容的另外两个方法是readline和readlines。
readline()每次读取一行,当前位置移到下一行;
readlines()读取整个文件所有行,保存在一个列表(list)变量中,每行作为一个元素;
read(size)从文件当前位置起读取size个字节(如果文件结束,就读取到文件结束为止),如果size是负值或省略,读取到文件结束为止,返回结果是一个字符串。
5.close()方法
操作完文件,一定要关闭文件。关闭文件就是用这个close方法
3 读取txt获取配置信息
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import cv2
import os
import RPi.GPIO as GPIO
import time
import requests
url='http://www.baidu.com/' # http报警网址
tel='12366669999'
peoplemax=8 #最大报警人数
videopath=0 #视频地址 0-1USB相机
f = open('/home/pi/Desktop/info', mode='r+', encoding='utf-8') # 打开txt文件,以‘utf-8’编码读取
lines = f.readlines() # 以行的形式进行读取文件
for line in lines:
a=line.strip().split('-') # x.strip()#除去每行的换行符 按照:分割
b = a[0:1] # list--str
c = "".join(b).strip() # 去除空格
if c=='peoplemax':
b = a[1:2] # 这是选取需要读取的位数
c="".join(b).strip() # 去除空格
peoplemax=int(c)
print(c)
if c=='tel':
b = a[1:2] # 这是选取需要读取的位数
c="".join(b).strip() # 去除空格
tel=str(c)
print(c)
if c=='url':
b = a[1:2] # 这是选取需要读取的位数
c="".join(b).strip() # 去除空格
url=str(c)
print(c)
if c=='videopath':
b = a[1:2] # 这是选取需要读取的位数
c="".join(b).strip() # 去除空格
if c=='0':
c=int(0)
else :
videopath=str(c)
print(c)
f.close()
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
pinA1=26
GPIO.setup(pinA1, GPIO.OUT) #设置脚为输出模式
time.sleep(1)
font = cv2.FONT_HERSHEY_SIMPLEX
#videopath='rtsp://admin:fhy145145@192.168.1.62/Streaming/Channels/1' #海康
cam = cv2.VideoCapture(videopath)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
count=0
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray, 1.3, 5)
count=0
for (x,y,w,h) in faces :
count=count+1
cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)
if count>peoplemax:
msg='alram!'
cv2.putText(img, msg, (5,80), font, 1, (0,0,255), 2)
GPIO.output(pinA1, GPIO.HIGH)
payload={'tel': str(tel), 'people_number':str(count),'state':'1'}
r = requests.get(str(url), params=payload)
print (r.status_code)
cv2.putText(img, str(r.status_code), (8,150), font, 1, (0,0,255), 2)
else:
msg='nomarl!'
cv2.putText(img, msg, (5,80), font, 1, (0,0,0), 2)
GPIO.output(pinA1, GPIO.LOW)
cv2.imshow('image', img)
cv2.waitKey(10)
cam.release()
cv2.destroyAllWindows()
txt
peoplemax - 8
tel - 1336666999
url - http://www.baidu.com/
videopath - 0
说明:
peoplemax 最大检测人数
tel 电话号码
url 上报网址
videopath 读取视频来源
USB免驱相机 0-1
海康网络相机 rtsp://admin:fhy145145@192.168.1.62/Streaming/Channels/1
账号 密码 相机IP 1主码流(1280×640) 2次码流(640×480)
浙公网安备 33010602011771号