參考大神鏈接
Python中的sys.argv[]
主要用于接受命令行的參數(shù),×sys.argv[0]表示代碼本身文件路徑×书斜,所以參數(shù)從1開(kāi)始
1.這里的File_Compare.py是代碼本身的文件路徑擂煞,參數(shù)是caffe 和cuda兩個(gè)目錄
比如,shell下運(yùn)行結(jié)果:
2.使用sys.argv[]進(jìn)行讀取參數(shù)的代碼
import sys
if len(sys.argv)<3:
print "You need t specify two directories:"
print sys.argv[0],"<directory1> <directory2>"
sys.exit()
directory1=sys.argv[1]
directory2=sys.argv[2]
print "Comparing:"
print directory1
print directory2
print
3.函數(shù)聲明:walk(top,topdown=True,onerror=None)
1>參數(shù)top表示需要遍歷的目錄樹(shù)的路徑
2>參數(shù)topdown的默認(rèn)值是"True",表示首先返回目錄樹(shù)下的文件混弥,然后在遍歷目錄樹(shù)的子目錄.Topdown的值為"False"時(shí),則表示先遍歷目錄樹(shù)的子目錄对省,
返回子目錄下的文件蝗拿,最后返回根目錄下的文件
3>參數(shù)onerror的默認(rèn)值是"None",表示忽略文件遍歷時(shí)產(chǎn)生的錯(cuò)誤.如果不為空,則提供一個(gè)自定義函數(shù)提示錯(cuò)誤信息后繼續(xù)遍歷或拋出異常中止遍歷
4>該函數(shù)返回一個(gè)元組蒿涎,該元組有3個(gè)元素哀托,這3個(gè)元素分別表示每次遍歷的路徑名,目錄列表和文件列表
示例:
import os
#don't loop the code ,make the code best use
for directory in [directory1,directory2]:
#check the imput if lawful
if not os.access(directory, os.F_OK):
print directory,"isn't a valid directory !"
sys.exit()
print "Directory",directory
#ergodic the dorectory
for iterm in os.walk(directory):
print iterm
print
分析:從根目錄開(kāi)始輸出一級(jí)目錄以及在根目錄下的文件劳秋,遞歸類似仓手。
4.os.access()
1>access()方法語(yǔ)法格式如下:
os.access(path, mode);
2>參數(shù)
path -- 要用來(lái)檢測(cè)是否有訪問(wèn)權(quán)限的路徑。
mode -- mode為F_OK玻淑,測(cè)試存在的路徑嗽冒,或者它可以是包含R_OK, W_OK和X_OK或者R_OK, W_OK和X_OK其中之一或者更多。
os.F_OK: 作為access()的mode參數(shù)补履,測(cè)試path是否存在添坊。
os.R_OK: 包含在access()的mode參數(shù)中 , 測(cè)試path是否可讀箫锤。
os.W_OK 包含在access()的mode參數(shù)中 贬蛙, 測(cè)試path是否可寫(xiě)。
os.X_OK 包含在access()的mode參數(shù)中 谚攒,測(cè)試path是否可執(zhí)行阳准。
import sys
if len(sys.argv)<3:
print "You need t specify two directories:"
print sys.argv[0],"<directory1> <directory2>"
sys.exit()
directory1=sys.argv[1]
directory2=sys.argv[2]
print "Comparing:"
print directory1
print directory2
print
import os
#don't loop the code ,make the code best use
for directory in [directory1,directory2]:
#check the imput if lawful
if not os.access(directory, os.F_OK):
print directory,"isn't a valid directory !"
sys.exit()
print "Directory",directory
#ergodic the dorectory
for iterm in os.walk(directory):
print iterm
print
import hashlib
def md5(file_path):
"""Return an md5 for a file """
if os.path.isdir(file_path):
return '1'
read_file=file(file_path)
the_hash=hashlib.md5()
for line in read_file.readlines():
the_hash.update(line)
return the_hash.hexdigest()
def directory_listing(dir_name):
"""Return all of the file inn a directory. """
dir_file_list={}
dir_root=None
dir_trim=0
#find out the root
for path,dirs,files in os.walk(dir_name):
if dir_root is None:
dir_root=path
dir_trim=len(dir_root)
print "dir ",dir_name,
print "root is ",dir_root
trimmed_path=path[dir_trim:]
if trimmed_path.startswith(os.path.sep):
trimmed_path=trimmed_path[1:]
for each_file in files+dirs:
file_path=os.path.join(trimmed_path,each_file)
dir_file_list[file_path]=True
return (dir_file_list,dir_root)
#main function
dir1_file_list,dir1_root=directory_listing(directory1)
dir2_file_list,dir2_root=directory_listing(directory2)
result={}
for file_path in dir2_file_list.keys():
if file_path not in dir1_file_list:
print file_path,"not found in the dirrectory1"
else:
print file_path,"found in the directory1 and 2 "
file1=os.path.join(dir1_root,file_path)
file2=os.path.join(dir2_root,file_path)
if md5(file1) != md5(file2):
result[file_path]="differ !"
else:
result[file_path]="is the same in the both"
del dir1_file_list[file_path]
for file_path,value in dir1_file_list.items():
if file_path not in result:
result[file_path]="not found in directory2 !"
print
for file_path,result_one in sorted(result.items()):
if os.path.sep not in file_path and "same" not in result_one:
print path,result
for path,result_one in sorted(result.items()):
if os.path.sep in file_path and "same" not in result_one:
print path,result_one