求一个PYTHON案例

Python014

求一个PYTHON案例,第1张

# 以下程序可能要安装OpenCV2.0(并编译好并配置好环境)以及Xvid解码器才能运行

# _*_coding: cp936_*_

import cv

capture = cv.CreateFileCapture("tmp.avi")

#请确保当前目录下有tmp.avi文件

fps = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FPS)

totalFrameNumber =cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_COUNT)

frameWidth = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH)

frameHeight = cv.GetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT)

print fps, totalFrameNumber, frameWidth, frameHeight

frame = cv.QueryFrame(capture)

while frame:

cv.ShowImage('Title', frame)

frame = cv.QueryFrame(capture)

if cv.WaitKey(1000 / fps) == 27:# ESC键退出视频播放

cv.DestroyWindow('Title')

break

一个文件夹整理工具wxPython版本(不清楚软件用法情况下可以打开该软件看看界面但请不要使用里面的功能,后果自负。)

# -*- coding: cp936 -*-

# file:aa.py

import os, shutil, sys

import wx

sourceDir = ''

sourceFiles = ''

preWord = 0

def browse(event):

global sourceDir, textPreWord, textDirName

dialog = wx.DirDialog(None, u'选择待处理文件夹', style = wx.OPEN)

if dialog.ShowModal() == wx.ID_OK:

sourceDir = dialog.GetPath().strip('\"')

textDirName.SetLabel(sourceDir)

dialog.Destroy()

textPreWord.SetFocus()

def okRun(event):

global preWord, sourceDir, sourceFiles, textPreWord

preWord = int(textPreWord.GetValue())

if preWord <= 0:

wx.MessageBox(u'请正确输入前缀字符个数!', u'出错啦', style = wx.ICON_ERROR | wx.OK)

textPreWord.SetFocus()

return

sourceDir = textDirName.GetValue()

sourceFiles = os.listdir(sourceDir)

for currentFile in sourceFiles:

tmp = currentFile

currentFile = sourceDir + '\\' + currentFile

currentFile = currentFile.strip('\"')

if os.path.isdir(currentFile):

continue

targetDir = '%s\\%s'%(sourceDir, tmp[:preWord])

if not os.path.exists(targetDir):

os.mkdir(targetDir)

shutil.move(currentFile, targetDir)

wx.MessageBox(u'任务完成!', u'好消息', style = wx.ICON_ERROR | wx.OK)

def onChange(event):

global dir1, textDirName

p = dir1.GetPath()

textDirName.SetLabel(p)

app = wx.App(0)

win = wx.Frame(None, title = u'文件整理', size = (400, 450))

bg = wx.Panel(win)

btnBrowse = wx.Button(bg, label = u'浏览')

btnBrowse.Bind(wx.EVT_BUTTON, browse)

btnRun = wx.Button(bg, label = u'整理')

btnRun.Bind(wx.EVT_BUTTON, okRun)

dir1 = wx.GenericDirCtrl(bg, -1, dir='', style=wx.DIRCTRL_DIR_ONLY)

tree = dir1.GetTreeCtrl()

dir1.Bind(wx.wx.EVT_TREE_SEL_CHANGED, onChange, id = tree.GetId())

textPreWord = wx.TextCtrl(bg)

textDirName = wx.TextCtrl(bg)

textPreWord.SetFocus()

class MyFileDropTarget(wx.FileDropTarget):#声明释放到的目标

def __init__(self, window):

wx.FileDropTarget.__init__(self)

self.window = window

def OnDropFiles(self, x, y, filenames):#释放文件处理函数数据

for name in filenames:

if not os.path.isdir(name):

wx.MessageBox(u'只能选择文件夹!', '出错啦', style = wx.ICON_ERROR | wx.OK)

return

self.window.SetValue(name)

dt = MyFileDropTarget(textDirName)

textDirName.SetDropTarget(dt)

hbox0 = wx.BoxSizer()

hbox0.Add(dir1, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)

hbox1 = wx.BoxSizer()

hbox1.Add(textDirName, proportion = 1, flag = wx.EXPAND, border = 5)

hbox1.Add(btnBrowse, proportion = 0, flag = wx.LEFT, border = 5)

hbox2 = wx.BoxSizer()

hbox2.Add(textPreWord, proportion = 1, flag = wx.EXPAND, border = 5)

hbox2.Add(btnRun, proportion = 0, flag = wx.LEFT, border = 5)

vbox = wx.BoxSizer(wx.VERTICAL)

vbox.Add(hbox0, proportion = 1, flag = wx.EXPAND | wx.ALL, border = 5)

vbox.Add(wx.StaticText(bg, -1, u'\n 选择待处理文件夹路径:'))

vbox.Add(hbox1, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)

vbox.Add(wx.StaticText(bg, -1, u'\n\n 前缀字符个数:'))

vbox.Add(hbox2, proportion = 0, flag = wx.EXPAND | wx.ALL, border = 5)

vbox.Add(wx.StaticText(bg, -1, u'\n'))

bg.SetSizer(vbox)

win.Center()

win.Show()

app.MainLoop()

#py2exe打包代码如下:

from distutils.core import setup

import py2exe

setup(windows=["aa.py"])

打包好了之后有18Mb。

一个文件内容批量替换摸查器(wxPython版本):

# _*_ coding:cp936 _*_

from string import join, split

import os

import wx

def getAllFiles(adir):

tmp = []

for parent, dirs, files in os.walk(adir):

for afile in files:

tmp.append(os.path.join(parent, afile))

return tmp

def browse(event):

dialog = wx.DirDialog(None, '选择待处理文件夹', style=wx.OPEN)

if dialog.ShowModal() == wx.ID_OK:

aDir = dialog.GetPath()

textDir.SetLabel(aDir)

dialog.Destroy()

def check():

promp.SetForegroundColour('#FF0000')

promp.SetLabel(' 搜索中...')

listFound.ClearAll()

tmp = []

files = getAllFiles(textDir.GetValue())

for filename in files:

try:

afile = open(filename, 'r')

except IOError:

print '打开文件错误。'

continue

try:

content = afile.read().decode('utf-8')

afile.close()

except:

try:

afile = open(filename, 'r')

content = afile.read().decode('cp936')

afile.close()

except:

continue

if textNeedChar.GetValue() in content:

tmp.append(filename)

listFound.InsertStringItem(0, filename)

parentDir.SetForegroundColour('#FF0000')

# parentDir.SetLabel(' 正在搜索:'+filename)

return tmp

def onFind(event):

check()

parentDir.SetLabel(' 搜索结果:')

promp.SetLabel(' 任务完成。')

wx.MessageBox(listFound.GetItemCount() and '任务完成,找到%d个文件。' % listFound.GetItemCount()

or '未找到包含"%s"的文件!' % textNeedChar.GetValue().encode('cp936'), '查找结束',

style=wx.ICON_INFORMATION | wx.OK)

def onReplace(event):

dg = wx.TextEntryDialog(bg, '替换成:', '提示', style=wx.OK | wx.CANCEL)

if dg.ShowModal() != wx.ID_OK:

return

files = check()

userWord = dg.GetValue()

#if not userWord:

#wx.MessageBox('请输入要替换为何字!', style=wx.ICON_ERROR | wx.OK)

#onReplace(event)

#return

for filename in files:

try:

afile = open(filename, 'r')

except IOError:

print '打开文件错误。'

continue

try:

content = afile.read().decode('utf-8')

afile.close()

except:

try:

afile = open(filename, 'r')

content = afile.read().decode('cp936')

afile.close()

except IOError:

print '打开文件错误。'

continue

content = content.replace(textNeedChar.GetValue(), userWord)

try:

content = content.encode('utf-8')

except:

pass

open(filename, 'w').write(content)

parentDir.SetLabel(' 替换的文件:')

promp.SetLabel(' 任务完成。')

wx.MessageBox(listFound.GetItemCount() and '任务完成,替换了%d个文件。' % listFound.GetItemCount()

or '未找到包含"%s"的文件!' % textNeedChar.GetValue().encode('cp936'), '替换结束', style=wx.ICON_INFORMATION | wx.OK)

app = wx.App(0)

win = wx.Frame(None, title='文件整理', size=(400, 450))

bg = wx.Panel(win)

btnBrowse = wx.Button(bg, label='浏览')

btnBrowse.Bind(wx.EVT_BUTTON, browse)

btnFind = wx.Button(bg, label='查找')

btnFind.Bind(wx.EVT_BUTTON, onFind)

btnFind.SetDefault()

btnReplace = wx.Button(bg, label='替换')

btnReplace.Bind(wx.EVT_BUTTON, onReplace)

textNeedChar = wx.TextCtrl(bg, value='你好')

textDir = wx.TextCtrl(bg, value='E:\Workspace\Python\Python\e')

parentDir = wx.StaticText(bg)

promp = wx.StaticText(bg)

listFound = wx.ListCtrl(bg, style=wx.VERTICAL)

class MyFileDropTarget(wx.FileDropTarget):#声明释放到的目标

def __init__(self, window):

wx.FileDropTarget.__init__(self)

self.window = window

def OnDropFiles(self, x, y, filenames):#释放文件处理函数数据

for name in filenames:

'''if not os.path.isdir(name):

wx.MessageBox('只能选择文件夹!', '出错啦', style=wx.ICON_ERROR | wx.OK)

return '''

self.window.SetValue(name)

dt = MyFileDropTarget(textDir)

textDir.SetDropTarget(dt)

hbox1 = wx.BoxSizer()

hbox1.Add(textDir, proportion=1, flag=wx.EXPAND, border=5)

hbox1.Add(btnBrowse, proportion=0, flag=wx.LEFT, border=5)

hbox2 = wx.BoxSizer()

hbox2.Add(textNeedChar, proportion=1, flag=wx.EXPAND, border=5)

hbox2.Add(btnFind, proportion=0, flag=wx.LEFT, border=5)

hbox2.Add(btnReplace, proportion=0, flag=wx.LEFT, border=5)

vbox = wx.BoxSizer(wx.VERTICAL)

vbox.Add(wx.StaticText(bg, -1, '\n 选择待处理文件夹(可拖动文件夹到下面区域中):'))

vbox.Add(hbox1, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

vbox.Add(wx.StaticText(bg, -1, '\n\n 要替换 / 查找的文字:'))

vbox.Add(hbox2, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

vbox.Add(promp, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

vbox.Add(parentDir, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)

vbox.Add(listFound, proportion=1,

flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)

bg.SetSizer(vbox)

win.Center()

win.Show()

app.MainLoop()

sal=float(input('请输入当月税前工资:'))

S=sal-3500

if 0<S<=1500:

L=S*0.03

print('应纳税%.2f元'%L)

elif 1500<S<=4500:

L=1500*0.03+(S-1500)*0.1

print('应纳税%.2f元'%L)

elif 4500<S<=9000:

L=1500*0.03+3000*0.1+(S-4500)*0.2

print('应纳税%.2f元'%L)

elif 9000<S<=35000:

L=1500*0.03+3000*0.1+4500*0.2+(S-9000)*0.25

print('应纳税%.2f元'%L)

elif 35000<S<=55000:

L=1500*0.03+3000*0.1+4500*0.2+26000*0.25+(S-35000)*0.3

print('应纳税%.2f元'%L)

elif 55000<S<=80000:

L=1500*0.03+3000*0.1+4500*0.2+26000*0.25+20000*0.3+(S-55000)*0.35

print('应纳税%.2f元'%L)

elif S>80000:

L=1500*0.03+3000*0.1+4500*0.2+26000*0.25+20000*0.3+25000*0.35+(S-80000)*0.45

print('应纳税%.2f元'%L)

else:

print('未到起征点,不用交税')