配置文件以及目录遍历
来源:互联网 发布:python中help的用法 编辑:程序博客网 时间:2024/06/05 16:31
#/usr/bin/python
#-*- coding:utf8 -*-
import difflib
import os
f1 = open("test1.txt", "r")
f2 = open("test2.txt", "r")
src = f1.read()
dst = f2.read()
print src
print dst
f1.close()
f2.close()
#任意类型序列的比较 (可以比较字符串)
#详细资料 http://www.pythonclub.org/python-basic/difflib
s = difflib.SequenceMatcher(lambda x: x=="", src, dst)
print s
for tag, i1, i2, j1, j2 in s.get_opcodes():
print ("%s src[%d:%d]=%s dst[%d:%d]=%s" % (tag, i1,i2, src[i1:i2], j1, j2,dst[j1:j2]))
#现在进行配置文件的读取 从网上找的一个例子啊 内容如下
#[ODBC 32 bit Data Sources]
#ms access database = Microsoft Access Driver (*.mdb) (32 λ)
#dbase files = Microsoft dBase Driver (*.dbf) (32 λ)
#excel files = Microsoft Excel Driver (*.xls) (32 λ)
#[MS Access Database]
#driver32 = C:\WINDOWS\system32\odbcjt32.dll
#[dBASE Files]
#driver32 = C:\WINDOWS\system32\odbcjt32.dll
#[Excel Files]
#driver32 = C:\WINDOWS\system32\odbcjt32.dll
import ConfigParser
config = ConfigParser.ConfigParser()
config.add_section("ODBC Driver Count")
config.set("ODBC Driver Count", "count", 2)
f = open("ODBC.ini", "a+")
config.write(f)
f.close()
#可以查看到文件已经添加了一个count
#现在删除一个记录
config.read("ODBC.ini")
config.remove_option("ODBC Driver Count", "count")
config.remove_section("ODBC Driver Count")
f = open("ODBC.ini", "w+")
config.write(f)
f.close()
#可以看见count记录没有了
#现在遍历一个文件夹下的所有文件
def ShowDir(path):
list_file = os.listdir(path)
for p in list_file:
pathname = os.path.join(path, p)
if not os.path.isfile(pathname): #如果是文件
ShowDir(pathname) #继续遍历
else:
print pathname #否则打印
path = "/home/python/"
ShowDir(path)
print
print
print
#使用walk函数来遍历
def ShowDir2(path):
for root, dirs, files in os.walk(path, True):
for filepath in files:
print os.path.join(root, filepath)
ShowDir2(path)
print
print
print
#再来一种
def ShowDir3(path, dirname, names):
for filepath in names:
print os.path.join(dirname, filepath)
os.path.walk(path, ShowDir3, ())
#-*- coding:utf8 -*-
import difflib
import os
f1 = open("test1.txt", "r")
f2 = open("test2.txt", "r")
src = f1.read()
dst = f2.read()
print src
print dst
f1.close()
f2.close()
#任意类型序列的比较 (可以比较字符串)
#详细资料 http://www.pythonclub.org/python-basic/difflib
s = difflib.SequenceMatcher(lambda x: x=="", src, dst)
print s
for tag, i1, i2, j1, j2 in s.get_opcodes():
print ("%s src[%d:%d]=%s dst[%d:%d]=%s" % (tag, i1,i2, src[i1:i2], j1, j2,dst[j1:j2]))
#现在进行配置文件的读取 从网上找的一个例子啊 内容如下
#[ODBC 32 bit Data Sources]
#ms access database = Microsoft Access Driver (*.mdb) (32 λ)
#dbase files = Microsoft dBase Driver (*.dbf) (32 λ)
#excel files = Microsoft Excel Driver (*.xls) (32 λ)
#[MS Access Database]
#driver32 = C:\WINDOWS\system32\odbcjt32.dll
#[dBASE Files]
#driver32 = C:\WINDOWS\system32\odbcjt32.dll
#[Excel Files]
#driver32 = C:\WINDOWS\system32\odbcjt32.dll
import ConfigParser
config = ConfigParser.ConfigParser()
config.add_section("ODBC Driver Count")
config.set("ODBC Driver Count", "count", 2)
f = open("ODBC.ini", "a+")
config.write(f)
f.close()
#可以查看到文件已经添加了一个count
#现在删除一个记录
config.read("ODBC.ini")
config.remove_option("ODBC Driver Count", "count")
config.remove_section("ODBC Driver Count")
f = open("ODBC.ini", "w+")
config.write(f)
f.close()
#可以看见count记录没有了
#现在遍历一个文件夹下的所有文件
def ShowDir(path):
list_file = os.listdir(path)
for p in list_file:
pathname = os.path.join(path, p)
if not os.path.isfile(pathname): #如果是文件
ShowDir(pathname) #继续遍历
else:
print pathname #否则打印
path = "/home/python/"
ShowDir(path)
#使用walk函数来遍历
def ShowDir2(path):
for root, dirs, files in os.walk(path, True):
for filepath in files:
print os.path.join(root, filepath)
ShowDir2(path)
#再来一种
def ShowDir3(path, dirname, names):
for filepath in names:
print os.path.join(dirname, filepath)
os.path.walk(path, ShowDir3, ())
QQ交流群: 204944806
0 0
- 配置文件以及目录遍历
- 目录遍历以及特殊字符
- 数据库分页以及目录遍历
- Tomcat目录结构以及配置文件说明
- linux 长目录快捷方式以及各个配置文件作用
- 【mysql】mycat目录结构以及主要配置文件解读
- 遍历指定目录下的所有文件和目录, 以及子目录下的文件和目录
- php遍历整个目录的文件以及文件夹
- Shell脚步中的递归函数以及遍历目录
- PHP目录以及文件遍历类欢迎大家拍砖
- paip.c++ qt 目录遍历以及文件操作
- Win32非递归遍历和搜索文件以及目录算法
- 遍历目录
- 遍历目录
- 遍历目录
- 遍历目录
- 遍历目录
- 目录遍历
- 数据库
- knockout简单实用教程3
- Redis学习笔记
- 幂集问题(组合问题)
- 给列表中每个元素加上同样的字符串
- 配置文件以及目录遍历
- setfacl设置特定目录的权限
- CSRF攻击与防御
- CSRF攻击与防御
- Unity3D游戏开发之游戏模型制作:机器人
- HTML/css碎片知识
- Window 通过cmd查看端口占用、相应进程、杀死进程等的命令
- CSRF攻击与防御
- 大数据面试题汇总