Python初步了解(八)

初级的了解就这些了,接下来就是个人喜欢的学习了,可以看网上的项目进行实际学习,我个人喜欢从小项目练手,逐渐研究大点的,Python能做游戏,我喜欢;Python能搞数据,我喜欢;Python能搞运维,这我就不是爸爸了么

一、OS模块

通过代码加注释理解

import os                        #导入os模块s
print(os.environ())              #输出系统环境变量
# print(os.environ.get('path'))  #输出系统环境变量

# print(os.curdir)

path = r'C:\Users\xlg\Desktop\县区列表list.txt'
# print(os.path.splitext(path))

# print(os.path.exists(path))

# print(os.path.dirname(path))
# print(os.path.basename(path))
# print(os.path.abspath('./1os.py'))
# print(os.path.split(path))

二、堆栈队列(代码模拟)

列表实现栈的结构
#列表实现栈的结构

mylist = []
str1 = 'abcedfghi'
#进栈的顺序
for i in str1:
    mylist.append(i)
#出栈的顺序
print(mylist.pop())
#栈内结果
print(mylist)

~~~

#### 利用栈的原理实现,文件内文件展示

~~~
import os
path = r'C:\...'                                #目标文件夹位置

filePathList = []
filePathList.append(path)

while len(filePathList) != -1:
    localPath = filePathList.pop()
    localListDir = os.path.listdir(localPath)   #将列表该目录下的文件名返回列表

    for fileName in localListDir:
        newPath = os.path.join(localPath,fileName)
        if os.path.isdir(newPath):
            filePathList.append(newPath)
        elif os.path.isfile(newPath)
            print('文件信息',fileName)
队列代码演示
import collections

que = collections.deque()   #使用队列

str1 = 'abcde'

for i in str1:
    que.append(i)

print(que.popleft())
print(que)

#结果:
#a
#deque(['b', 'c', 'd', 'e', 'f', 'g'])
利用队列原理,实现文件夹中所有文件名显示
import collections
import os

path = 'C:\....'                            #文件夹目标路径

que = collections.deque()                   #声明队列
que.append(path)

while len(que) != 0:
    localPath = que.popleft
    fileList = os.path.listdir(localPath)   #获取列表形式文件名

    for fileName in filelist:
        newPath = os.path.join(path,fileName)
        if os.path.isdir():
            que.append(newPath)
        elif os.path.isfile():
            print('文件名',fileName)
~~~

## 三、装饰器

~~~
#原理代码
def demo(x):
    def inner(arg):
        print('demodemodemo',arg)
        x(arg)
    return inner

def func(arg):
    print('funcfuncfunc',arg)

func = demo(func)
func(30)

#结果:
#demodemodemo 30
#funcfuncfunc 30
~~~
利用装饰器实现以上代码
def demo(x):
    def inner(arg):
        print('demodemodemo',arg)
        x(arg)
    return inner

@demo  # == func = demo(func)
def func(arg):
    print('funcfuncfunc',arg)

func(30)

#结果:
#demodemodemo 30
#funcfuncfunc 30

四、时间模块

import time

print(time.strftime('%Y-%m-%d %H:%M:%S'))
#结果 2017-08-12 10:18:04

mytime = "2017-08-11 20:48:53"
#返回时间的元组
x = time.strptime(mytime,"%Y-%m-%d %H:%M:%S") 
# 元组转 中国时间格式
print(time.strftime('%Y-%m-%d %H:%M:%S',x))

#将时间的元组 转换成 秒数
mytimestime = time.mktime(x)
print(mytimestime)

小实例1:更改植物大战僵尸(阳光值)

1、准备文件

文件名注意事项解决方案
植物大战僵尸文件
pywin32-221.win32-py3.6.exe需要确认安装成功若安装失败,修改python、该文件路径下的中文字符
Spy.exe用于捕捉标题、类
辅助工具获取阳光地址

2、整理数据

#标题:植物大战僵尸中文版
#类:MainWindow
#最高权限值:(0x000F0000|0x00100000|0xFFF)
~~~

3、编写代码

~~~
import win32process
import win32gui
import cytpes
import win32api

#查找窗体
window = win32gui.FIndWindow('MainWindow','植物大战僵尸中文版')
#当前窗体进程编号
hid,pid = win32process.GetWindowThreadProcessId(window)
#用最高权限打开进程
p = win32api.OpenProcess((0x000F0000|0x00100000|0xFFF),False,pid)
#C语言模块
date = ctypes.c_long()
#加载模块
localdll = ctypes.windll.LoadLibray('C:/Windows/System32/kernel32.dll')

#获取当前进程内存  234930512(阳光地址)
localdll.ReadProcessMemory(int(p),234930512,ctypes.byref(date),4,None)
#读取阳光值
print(date.value)

#改写当前进程内存
localdll.WriteProcessMemory(int(p),234930512,ctypes.byref(newdate),4,None)

小实例2:控制QQ窗体

代码实例:

import win32gui
import win32con
import random

qq = win32gui.FindWindow('TXGuiFoundation','QQ')

#第一种
for i in range(500):
    win32gui.SetWindowPos(qq,win32con.HWD_TOPMOST,0,0,i,i,win32con.SWP_SHOWWINDOW)

#第二种效果
while True:
    x = random.randrange(1000)
    y = random.randrange(1000)
    win32gui.SetWindowPos(qq, win32con.HWND_TOPMOST, x, 10, 400, 400, win32con.SWP_SHOWWINDOW)
    win32gui.SetWindowPos(feiq, win32con.HWND_TOPMOST, x, 300, 400, 400, win32con.SWP_SHOWWINDOW)

#第三种效果
while True:
    win32gui.ShowWindow(qq,win32con.SW_HIDE)
    time.sleep(1)
    win32gui.ShowWindow(qq,win32con.SW_SHOW)

小实例3:文本语音输出

import win32com.client

speaker = win32com.client.Dispatch('SAPI.SPVOICE')
speaker.Speak('文本内容')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值