[py]基础数据类型

xiaoxiao2021-02-28  127

传递字典到字符串

infor={'name':'maming','age':22} msg_format="%(name)s---%(age)d" print msg_format%(infor)

os.walk

for i in []: print '不输出' for i in '': print '不输出' for i in ' ': print '输出'

举个栗子:

C:\LOGS ├─app01 │ └─app01-L2 ├─app02 └─app03

0,输出root,观察怎麼walk的

import os for root,dirs,file in os.walk("c:/logs"): print root c:/logs c:/logs\app01 c:/logs\app01\app01-L2 c:/logs\app02 c:/logs\app03

1,输出dir import os for root, dirs, files in os.walk("C:/logs"): print dirs ['app01', 'app02', 'app03'] ['app01-L2'] [] [] [] 2,输出所有层次目录和子目录: import os for root, dirs, files in os.walk("C:/logs"): # #输出目录路径 for name in dirs: # print name print(os.path.join(root, name)) C:/logs\app01 C:/logs\app02 C:/logs\app03 C:/logs\app01\app01-L2

logs ├── access.log ├── app01 │ ├── app01-L2 │ └── app01.log ├── app02 │ └── app02.log └── app03

3,输出files import os for root, dirs, files in os.walk("C:/logs"): print files ['access.log'] ['app01.log'] [] ['app02.log'] [] 4,输出文件路径 import os for root, dirs, files in os.walk("C:/logs"): print dirs #输出文件目录下文件路径 for name in files: print(os.path.join(root, name)) C:/logs\access.log C:/logs\app01\app01.log C:/logs\app02\app02.log os获取环境变量,os 增删改查文件

os.getenv(“HOSTNAME”) os.remove os.removedirs os.mkdir os.rmdir os.path.isdir os.path.isfile os.path.exists() 获取路径名:os.path.dirname() 获取文件名:os.path.basename() 运行shell命令: os.system() 读取和设置环境变量:os.getenv() 与os.putenv() 函数 1,传元组 def fun(name,age): print "name:%s age:%s"%(name,age) # t=('lanny',24) t=(24,'lanny') fun(*t) name:24 age:lanny 2,传字典(无序的) def fun(name,age): print "name:%s age:%s"%(name,age) dic={"name":"lanny","age":24} fun(**dic) 举个栗子: db_config = { 'host' : 'localhost', 'user' : 'root', 'passwd' : 'zhuima', 'charset' : 'utf8', 'db': 'txl', } conn = MySQLdb.connect(**db_config) 3,传任意多个参数-元组 def fun(*l): print l fun(1,2,3) (1, 2, 3) 3,传任意多参数-字典 def fun(**dic): print dic fun(jack=1,mark=2) {'jack': 1, 'mark': 2}

4,

def fun(*l,**dic): print l,dic fun(18706773935,jack=1,mark=2)

字典形式传参

import time import os TIME_FORMAT="%b %d %Y %H:%M:%S" now = time.strftime(TIME_FORMAT, time.localtime()) data="this is data" MSG_FORMAT="%(now)s - %(data)s\n\n" msg = MSG_FORMAT % dict(now=now, data=data) print msg print msg print msg

函数:

def fun(): print 'hello, fun' return 1 if __name__ == '__main__': astr=fun() if astr: print 'hello' 时间日期 1, import time print time.ctime() print time.time() Sat May 06 10:39:21 2017 1408066927.208922 2, import time print time.strftime("%Y%m%d") # time.strftime('%Y-%m-%d %H:%M:%S') 20170506 3, import time def t(): start=time.time() time.sleep(3) end=time.time() print end-start t() 3.0 4, today=datetime.date.today() yesterday = (today - datetime.timedelta(days=1)).strftime('%b %d') 5, python计算明天的日期: from datetime import datetime from datetime import timedelta now = datetime.now() aDay = timedelta(days=1) now = now + aDay print now.strftime('%Y-%m-%d') 2017-05-07 python计算昨天时间: from datetime import datetime from datetime import timedelta now = datetime.now() aDay = timedelta(days=-1) now = now + aDay print now.strftime('%Y-%m-%d') 2017-05-09 执行命令 os.system("ln -s /etc/hosts /root/hosts") print os.popen("free -m").read() res=commands.getoutput("pwd") status,res=commands.getstatusoutput('free -m') print res subprocess.call('pwd',shell=True)

转载请注明原文地址: https://www.6miu.com/read-32992.html

最新回复(0)