配置檔案以及目錄遍歷
阿新 • • 發佈:2019-02-04
#/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