python判断路径是文件还是文件夹_Python检测不同目录下文件是否相同(代码有bug)...

Python中的sys.argv[]

主要用于接受命令行的参数,×sys.argv[0]表示代码本身文件路径×,所以参数从1开始

1.这里的File_Compare.py是代码本身的文件路径,参数是caffe 和cuda两个目录

比如,shell下运行结果:

64a5f39bb6ac

图片.png

2.使用sys.argv[]进行读取参数的代码

import sys

if len(sys.argv)<3:

print "You need t specify two directories:"

print sys.argv[0],""

sys.exit()

directory1=sys.argv[1]

directory2=sys.argv[2]

print "Comparing:"

print directory1

print directory2

print

3.函数声明:walk(top,topdown=True,οnerrοr=None)

1>参数top表示需要遍历的目录树的路径

2>参数topdown的默认值是"True",表示首先返回目录树下的文件,然后在遍历目录树的子目录.Topdown的值为"False"时,则表示先遍历目录树的子目录,

返回子目录下的文件,最后返回根目录下的文件

3>参数onerror的默认值是"None",表示忽略文件遍历时产生的错误.如果不为空,则提供一个自定义函数提示错误信息后继续遍历或抛出异常中止遍历

4>该函数返回一个元组,该元组有3个元素,这3个元素分别表示每次遍历的路径名,目录列表和文件列表

示例:

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

64a5f39bb6ac

运行结果

分析:从根目录开始输出一级目录以及在根目录下的文件,递归类似。

4.os.access()

1>access()方法语法格式如下:

os.access(path, mode);

2>参数

path -- 要用来检测是否有访问权限的路径。

mode -- mode为F_OK,测试存在的路径,或者它可以是包含R_OK, W_OK和X_OK或者R_OK, W_OK和X_OK其中之一或者更多。

os.F_OK: 作为access()的mode参数,测试path是否存在。

os.R_OK: 包含在access()的mode参数中 , 测试path是否可读。

os.W_OK 包含在access()的mode参数中 , 测试path是否可写。

os.X_OK 包含在access()的mode参数中 ,测试path是否可执行。

import sys

if len(sys.argv)<3:

print "You need t specify two directories:"

print sys.argv[0],""

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值