python 学习笔记

前一段时间接触了python,觉得好用方便,顺便学习下, 写一些工具来帮助自己的项目,再好的记性也会忘记,在此记下笔记,希望被别人看到后也会有所帮助。


1、判断字符串是否包含

#-*- coding: utf-8 -*-
tablename="const_value"

if tablename.count('const') <=0 :
	print tablename + "not const script"
else :
	print 'found const'
		
x = raw_input("please input any key to exit")

2、支持中文注释

在代码文件的第一行加上     #-*- coding: utf-8 -*-


3、获取当前执行脚本的路径,如果文件夹不存在创建文件夹

#-*- coding: utf-8 -*-

import os #获取路径

savepath = os.getcwd()
print savepath

folder = savepath + "\\testfolder\\"
#如果不存在文件夹,创建文件夹
if os.path.exists(folder) == False :
	os.makedirs(folder)

x = raw_input("please input any key to exit")


4、文件的保存于读取

#-*- coding: utf-8 -*-
import os #获取路径

savepath = os.getcwd()
print savepath

#读取配置信息
def LoadFile(filename):
    f = open(filename,'r')
    content =f.read()
    return content

#保存文件
def SaveToFile(filename,content):
    f = open(savepath+filename, 'w')
    f.writelines(list(content))
    f.close()
    print "[ "+filename+" ]-- file saved"
	
SaveToFile("\\test.txt","123456")

print LoadFile("test.txt");





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值