forked from chenguohui/AutomatePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequence.py
More file actions
29 lines (25 loc) · 732 Bytes
/
Copy pathsequence.py
File metadata and controls
29 lines (25 loc) · 732 Bytes
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
29
import os
import shutil
import re
def sequence(dir, prefix):
# dir exists
if not os.path.exists(dir):
print('Directory not exists.')
return
# list all file and get the number of files with prefix
filenum = 0
filelist = []
for file in os.listdir(dir):
if file.startswith(prefix):
filenum += 1
filelist.append(file)
filelist.sort()
# rename file
print(filelist)
for i in range(filenum):
srcfile = os.path.join(dir, filelist[i])
dstfile = os.path.join(dir, prefix + '%03d'%(i+1) + '.txt')
if srcfile != dstfile:
print(srcfile, dstfile)
shutil.move(srcfile, dstfile)
sequence('demo', 'spam')