python-7

1.下载 itchat 编译 安装 需要devel

import itchat   引用模块
itchat.auto_login()        登陆
itchat.send('hello itchat!',toUserName='filehelper')
发送消息  给  微信助手
import itchat
itchat.auto_login(hotReload=True)   热登陆  不用重复登陆
info=itchat.get_friends()    查看朋友   包括自己
print(info)
统计好友个数
import itchat
itchat.auto_login(hotReload=True)
info=itchat.get_friends()
print('%s num:%d' %(info[0]['NickName'],len(info)-1))
import itchat
itchat.auto_login()
info=itchat.get_friends()
print('%s num:%d' %(info[0]['NickName'],len(info)-1))

male=female=other=0
for friend in info[1:]:
    sex=friend['Sex']
    if sex==1:
        male +=1
    elif sex==2:
        female +=1
    else:
        other +=1
def my_name(*args):
    pass

print('%s man:%s' %(my_name,male))
print('%s woman:%s' %(my_name,female))
print('%s other:%s' %(my_name,other))

统计 男女比例


/usr/local/bin/bin/python3.6 /root/PycharmProjects/untitled/day07/python__01.py
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Loading the contact, this may take a little while.
Login successfully as 亦又
亦又 num:209
<function my_name at 0x7f3f4d57b048> man:114
<function my_name at 0x7f3f4d57b048> woman:77
<function my_name at 0x7f3f4d57b048> other:18

Process finished with exit code 0
import time

import itchat
itchat.auto_login()
name=itchat.search_friends('ronger')[0]['UserName']
while True:
    time.sleep(10)
    itchat.send('%s,hello' %('ronger'),toUserName=name)
    print('login....')

给好友发消息



/usr/local/bin/bin/python3.6 /root/PycharmProjects/untitled/day07/python__01.py
Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Loading the contact, this may take a little while.
Login successfully as 亦又
login....
login....
login....
login....
login....
login....
LOG OUT!
login....
login....
import time

import itchat
def main():
    itchat.auto_login()
    name=itchat.search_friends('ronger'[0]['UserName']     给指定好友发消息
    while True:
        time.sleep(1)
        itchat.send('%s,hello' %('ronger'),toUserName=name)
        print('login....')

if __name__=='__main__':   如果不是调用的函数 就执行
    main()
聊天小程序
import itchat
@itchat.msg_register(itchat.content.TEXT)
def hello(msg):
    print(msg)
    text=msg['Text']
    if 'you' in text:
        return 'Do you love me'
    elif 'yes' in text:
        return 'i love you too'
    else:
        return 'i love you'
    itchat.send(res,toUserName=fromUser)


itchat.auto_login()
itchat.run()
a=1
b=2
def add(x,y):
    return x+y
def mypower(x,y=2):
    return x**y
if __name__=='__main__':  如果不是引用的   就执行  可以作用多个
    print(add(1,2))
    print(mypower(2,3))





/usr/local/bin/bin/python3.6 /root/PycharmProjects/untitled/day07/python_bao.py
3
8

Process finished with exit code 0
a=1
b=2
def add(x,y):         被引用的函数
    return x+y
def mypower(x,y=2):
    return x**y
if __name__=='__main__':
    print(add(1,2))
    print(mypower(2,3))





import UserManage.hello
print(UserManage.hello.add(1,2))   引用





from UserManage import hello    引用
print(hello.add(2,2))
from hello import a
from hello import add     有这个 
from pack0.hello__01 import *     也可以这样写


import UserManage
print(UserManage.a)      就可以省略
打开文件,以读的方式打开文件,返回一个文件对象
f=open('/tmp/passwd')
print(f,type(f))



/usr/local/bin/bin/python3.6 /root/PycharmProjects/untitled/day09/wechat1/baocaozuo.py
<_io.TextIOWrapper name='/tmp/passwd' mode='r' encoding='UTF-8'> <class '_io.TextIOWrapper'>

Process finished with exit code 0
操作文件
content=f.read()
print(content)


print(f.readline())   11行 看


print(f.readlines())   以列表的方式查看



print([line.strip() for line in f.readlines()])   去空格 \n  .strp()


f.close()     关闭文件


f=open('/tmp/pass','w')  以写的方式打开     没有则自动创建
f.write('westos')
f.close()
f=open('/tmp/pass','w')  
# print(f.writable())
li=['java','c','python','qq']
lis=[i+'\n' for i in li]   \n  换行
f.writelines(lis)     写进去
f.close()

w 文件不存在,则自动创建;
只能写 不能读取文件
文件内容会被覆盖调,当以 w 方式打开

r 以读的方式打开,定位到文件开头 , 默认的 mode
r+ 以读写的方式打开,定位文件开头 , 可以写入内容到文件
w 以写的方式打开,打开文件的时候会清空文件的内容,并且不能读
w+ 以读写的方式打开,定位到文件头,并且打开文件的时候也会清空文件的内容
a 以写的方式打开,定位到文件的末尾,是一个追加的操作 , 但并不允许读
a+ 以读写的方式打开,定位到文件的末尾,追加的方式。
在使用以上 mode 打开文件的时候,如果增加了b 模式,表示以二进制方式打开
f=open('/tmp/pass','r')
print(f.read())



/usr/local/bin/bin/python3.6 /root/PycharmProjects/untitled/day09/wechat1/baocaozuo.py
java
c
python
qq

f=open(‘/tmp/pass’,’r+’) 可写入内容到文件
print(f.write(‘lalala’))

import os
def rename_suffix(dirname, old_suffix, new_suffix):
    if os.path.exists(dirname):
        suffix_lis=[filename for filename in os.listdir(dirname) if filename.endswith(old_suffix)]

        li1=[os.path.splitext(file)[0] for file in suffix_lis]

        # li=[os.rename(dirname+file+old_suffix, dirname+file+new_suffix) for file in basename_lis]
        for file1 in li1:
            old_filename=dirname+file1+old_suffix
            new_filename=dirname+file1+new_suffix

            os.rename(old_filename,new_filename)
    else:
        print('%s not exit' %(dirname))
rename_suffix('img/','.jpg','.png')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值