- 论坛徽章:
- 0
|
判断某目录及该目录下全部深层目录中,所有文件的文件名是否是独一无二的。如果出现重复,请屏幕打印出重复 ...
松饼熊 发表于 2011-04-13 18:30 ![]()
你的需求已完成,不过这个脚本用shell的话会更简单,但既然你要求用python那就用python帮你写了,已通过测试没问题,只需要运行 python Dir.py 后面跟你想要查的目录就可以了。
more Dir.py
import os,sys
#for i in range(1,20):
# Dir="dir0" + str(i)
# os.makedirs(Dir)
# Filename="file0" + str(i)
# F2=Dir + "/" + Filename
# open= file(F2,'wb')
# open.write('haha')
# open.close()
try:
TargetDir=sys.argv[1]
Command="find %s -type f > find.tmp" % TargetDir
os.system(Command)
FilterFile=file('find.tmp')
OnlyFileName=file('filename.tmp','wb')
while True:
line = FilterFile.readline()
a=line.split('/')[-1]
OnlyFileName.write(a)
if len(line) == 0:
break
OnlyFileName.close()
FilterFile.close()
os.system('rm find.tmp')
Sort="awk -F '|' '{a[$1]++}a[$1]==2{print $1}' filename.tmp > find2.tmp"
os.system(Sort)
if os.stat('find2.tmp') .st_size == 0:
print "No same filename found !"
sys.exit()
Find2Open=file('find2.tmp')
while True:
line=Find2Open.readline()
if len(line) == 0:
break
FindFile="find %s -name %s" % (TargetDir,line)
os.system(FindFile)
os.system('rm find2.tmp filename.tmp')
except IndexError:
print "please specify the directory!"
例如:
bjnppb01:~/scripts/TestDir # ls -R
.:
createdir.py dir010 dir012 dir014 dir016 dir018 dir02 dir04 dir06 dir08 f
dir01 dir011 dir013 dir015 dir017 dir019 dir03 dir05 dir07 dir09
./dir01:
file01
./dir010:
file010
./dir011:
file011
./dir012:
file012 file08
./dir012/file08:
./dir013:
file013
./dir014:
file014
./dir015:
file015
./dir016:
file016
./dir017:
file017
./dir018:
file018
./dir019:
file019
./dir02:
file02
./dir03:
file03
./dir04:
file04
./dir05:
file05
./dir06:
file06 file07
./dir07:
file07
./dir08:
file08
./dir09:
file09
bjnppb01:~/scripts/TestDir # python createdir.py . 以下是查出来的相同的文件,看能达到你的要求吗?
./dir06/file07
./dir07/file07 |
|