Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Python实用又常用函数工具方法分享

$
0
0

这是我自己写的一些python函数方法,工能有挺多都是比较实用的。只要是不追求完美代码的朋友,都可以适用。像是中文字符集、检测图片类型(类型,大小,分辨率)、检测文件大小路径、输入错误日志路径等等的一些小方法。有收集来的,有自己写的,统统集合在一些,做与了一个Python实用又常用函数的方法大集合,分享给大家。


Python实用又常用函数工具方法分享

python中文字符集函数方法

?

# -*- coding: utf-8 -*-

import numpy

import Image

import os

import sys

def encodeChinese(msg):

type = sys.getfilesystemencoding()

return msg.decode( 'UTF-8' ).encode( type )

python检测图片类型函数方法

?

def check_imgMode(filedir):

try :

img = Image. open (filedir)

return img.mode

except :

errInfo = encodeChinese( '这不是图片: ' ) + str (filedir) + '\n'

print errInfo

return errInfo

python检测文件后缀,返回文件后缀方法

?

def check_fileMode(filedir):

fPostfix = os.path.splitext(filedir)[ 1 ]

return fPostfix

python打开一个图片文件的方法

?

def open_imgFile(filedir):

im = Image. open (filedir)

im.load()

return im

python输入被检测文件夹路径方法

?

def input_rootdir():

print encodeChinese( '请输入要检测的文件夹路径: ' )

rootdir = raw_input ()

print rootdir

return rootdir

python输入错误日志路径方法

?

def input_logdir():

print encodeChinese( '请输入错误日志路径: ' )

logdir = raw_input ()

print logdir

return logdir

输入处理完成图片保存路径

?

def input_targetdir():

print encodeChinese( '请输入处理完成后文件保存路径: ' )

targetdir = raw_input ()

return targetdir

检测文件大小

?

def check_fileSize(filedir): #www.iplaypython.com

try :

f = open (filedir, 'rb' )

f.seek( 0 , 2 )

fSize = f.tell()

#print 'fSize: ' + str(type(fSize))

#f.close()

return f.tell()

except :

print encodeChinese( '获取文件大小时发生错误' )

python检测图片alpha通道方法

?

def check_png_alpha(rootdir,errLogDir):

errFile = open (errLogDir, 'w' )

for parent,dirnames,filenames in os.walk(rootdir):

for filename in filenames:

fName = filename

filename = rootdir + os.sep + filename

if check_fileMode(filename) = = '.png' :

if check_imgMode(filename) = = 'RGBA' :

print filename

try :

img

Viewing all articles
Browse latest Browse all 9596

Trending Articles