os 的使用
import os
current_path = os.getcwd() # get the current path
father_path = os.path.dirname(current_path)# the parent path
os.path.dirname(father_path) # get the grandfather path
os.path.join(current_path, "filename.txt") # the filename.txt path
# judge file exist
if os.path.exists("your file.txt")
os.remove("your file.txt")
# or
os.mkdir("your file.txt")
others:
__import__(module_name)
#__import__() 函数用于动态加载类和函数 。
#如果一个模块经常变化就可以使用 __import__() 来动态载入。
print/format格式化输出
print("height is :{:1.2f}".format(1.70))
print("height: %1.2f"%(1.70))
print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
# 通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site))
# 通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
数值 | 格式 | 输出 | 描述 |
---|---|---|---|
13 | {:>10d} | . 13 | 右对齐 (默认, 宽度为10) |
12 | {:<10d} | 13 | 左对齐宽度为10 |