让你盯着看的东西,适应你自己的需求:
让我们创建一些文件:
$ touch a-b-2013-02-12-16-38-54-{a..f}.png
$ ls
a-b-2013-02-12-16-38-54-a.png a-b-2013-02-12-16-38-54-c.png a-b-2013-02-12-16-38-54-e.png f.py
a-b-2013-02-12-16-38-54-b.png a-b-2013-02-12-16-38-54-d.png a-b-2013-02-12-16-38-54-f.png
一些蟒蛇
#!/usr/bin/env python
import glob, os
files = glob.glob('*.png')
for f in files:
# get the character before the dot
d = f.split('-')[-1][0]
#create directory
try:
os.mkdir(d)
except OSError as e:
print 'unable to creade dir', d, e
#move file
try:
os.rename(f, os.path.join(d, f))
except OSError as e:
print 'unable to move file', f, e
让我们运行它
$ ./f.py
$ ls -R
.:
a b c d e f f.py
./a:
a-b-2013-02-12-16-38-54-a.png
./b:
a-b-2013-02-12-16-38-54-b.png
./c:
a-b-2013-02-12-16-38-54-c.png
./d:
a-b-2013-02-12-16-38-54-d.png
./e:
a-b-2013-02-12-16-38-54-e.png
./f:
a-b-2013-02-12-16-38-54-f.png