rename file according to some file in them

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import os
for root,dirs,files in os.walk('./'):
for name in dirs:
try:
n=0
dir_cur = os.path.join(root,name)
f = open(dir_cur + '/dirname.txt')
name = f.readline()[:-1]
if name != dir_cur:
os.rename(dir_cur,name)
else:
f.close()
continue
except OSError:
error = True
while error:
try:
tmp = name + str(n)
os.rename(dir_cur,tmp)
error = False
except:
n+=1
name = tmp
f.close()
except Exception:
pass
print dir_cur,name