1,先说说今天体会最深刻的一点:写python代码,一个好的编辑器至关重要,这其中VIM确实是最佳选择。由于python是根据缩进来判断上下文的,因此tab和空格使用不当会害死你的。一段代码在Notepad++里看起来对齐格式没问题,可就是死活编译不过,来到vim下一看,damn it,格式是乱的。本想用4个空格替换掉所有的tab,可该死的Notepad++居然并没有全部替换掉,再换UltraEdit32,依然如故,不得已,最后换了EmEditor,比前面两个强多了,
2,python实现遍历文件夹下所有文件
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
for
root,dirs,files
in
os.walk(sdir,topdown
=
False):
for file in files:
full_path = sdir + file
if os.path.exists(full_path) == True:
# processfile
for file in files:
full_path = sdir + file
if os.path.exists(full_path) == True:
# processfile
3,python处理命令行参数
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
import
sys
import getopt
while true:
try :
opts,args = getopt.getopt(sys.argv[ 1 :], " hs:t:d:i " ,[ " help " , " source= " , " targe= " , " ip= " ])
for opt,arg in opts:
if opt in ( " -h " , " --help " ):
print ( " showhelpinfo/n " )
elif opt in ( " -t " , " --targe " ):
target = arg
elif opt in ( " -s " , " --source " ):
source = arg
elif opt in ( " -e " , " --exit " ):
sys.exit(0)
except getopt.GetoptError:
sys.exit( - 1 )
import getopt
while true:
try :
opts,args = getopt.getopt(sys.argv[ 1 :], " hs:t:d:i " ,[ " help " , " source= " , " targe= " , " ip= " ])
for opt,arg in opts:
if opt in ( " -h " , " --help " ):
print ( " showhelpinfo/n " )
elif opt in ( " -t " , " --targe " ):
target = arg
elif opt in ( " -s " , " --source " ):
source = arg
elif opt in ( " -e " , " --exit " ):
sys.exit(0)
except getopt.GetoptError:
sys.exit( - 1 )
4,python中的信号处理机制
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/81178cc93a2a3bb5048d90d76e7ec935.gif)
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->
import
signal
def handler(signum,frame):
print ' Signalhandlercalledwithsignal ' ,signum
raise TimeOutError, " TimeOut! "
try :
# Setthesignalhandleranda1-secondalarm
signal.signal(signal.SIGALRM,handler)
signal.alarm( 1 )
# Thiswhileloophangindefinitely
while True:
print ' a ' ,
signal.alarm(0) # Disablethealarm
except :
print
print ' Timeoutcaught! '
def handler(signum,frame):
print ' Signalhandlercalledwithsignal ' ,signum
raise TimeOutError, " TimeOut! "
try :
# Setthesignalhandleranda1-secondalarm
signal.signal(signal.SIGALRM,handler)
signal.alarm( 1 )
# Thiswhileloophangindefinitely
while True:
print ' a ' ,
signal.alarm(0) # Disablethealarm
except :
print ' Timeoutcaught! '
5,mysql的几个常用点:1)truncate table tablename用来删除表中所有数据。2)limit相当于其他数据库中的top,取指定条数记录。3)desc 表名用来查看表结构 4)若表中字段是关键字,例如group,那么需要使用`group`(不是单引号,是数字1旁边那个键)。