python5 : .strip() / os.path...() /reload(sys)

.strip()

用于移除字符串头尾指定的字符(默认为空格)或字符序列。只要头尾包含有指定字符序列中的字符就删除。例如:12、21

注意该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。


os.path.exists() 用于判断变量、文件等是否存在

os.path.isdir()函数判断某一路径是否为目录


ConfigParser 是Python自带的模块, 用来读写配置文件, 用法及其简单。

# -* - coding: UTF-8 -* -  
import ConfigParser

conf = ConfigParser.ConfigParser()
conf.read("c:\\test.conf")

# 获取指定的section, 指定的option的值
name = conf.get("section1", "name")
print(name)
age = conf.get("section1", "age")
print age

#获取所有的section
sections = conf.sections()
print sections

#写配置文件

# 更新指定section, option的值
conf.set("section2", "port", "8081")

# 写入指定section, 增加新option的值
conf.set("section2", "IEPort", "80")

# 添加新的 section
conf.add_section("new_section")
conf.set("new_section", "new_option", "http://www.cnblogs.com/tankxiao")

# 写回配置文件
conf.write(open("c:\\test.conf","w"))


AttributeError: 'module' object has no attribute 'setdefaultencoding'的错误时

import sys  
reload(sys)  
sys.setdefaultencoding('utf8')   

读写文件原来

try:
    f = open('/path/to/file', 'r')
    print(f.read())
finally:
    if f:
        f.close()

现在:

with open('/path/to/file', 'r') as f:
    print(f.read())

'r' : read

'w' : write

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值