前一段时间接触了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");