Think before you speak, read before you think.

Tag: snippet

  • python获取昨天日期时间戳

    snippet import datetime yesterday = datetime.datetime.now() – datetime.timedelta(days=1) yesterday_formatted = yesterday.strftime(“%Y-%m-%d”) python 生成时间戳 import time now = int(time.time())

  • python遍历dictionary

    如代码 #!/usr/bin/env python #-*- coding:utf-8 -*- dict={“a”:”apple”,”b”:”banana”,”o”:”orange”} print “##########dict######################” for i in dict: print “dict[%s]=” % i,dict[i] print “###########items#####################” for (k,v) in dict.items(): print “dict[%s]=” % k,v print “###########iteritems#################” for k,v in dict.iteritems(): print “dict[%s]=” % k,v print “###########iterkeys,itervalues#######” for k,v in zip(dict.iterkeys(),dict.itervalues()): print “dict[%s]=” % k,v

  • python any判断一个对象是否为空

    any判断dict,list,tuple是不是为空 >>> eth = {“eth0″:”192.168.1.1”} >>> any(eth) True >>> eth = {} >>> any(eth) False

  • python获取某目录下的所有文件和子目录-os.listdir

    有时候需要获取某目录下的所有文件和子目录,从stackoverflow搜到了一段代码,需要时可以使用。 #!/usr/bin/python import os, sys # Open a file path = “/var/www/html/” dirs = os.listdir(path) # This would print all the files and directories for file in dirs: print file