-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_renumber.py
48 lines (43 loc) · 1.25 KB
/
_renumber.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import os.path
order = (
'hello_world',
'calculator',
'variables',
'types',
'lists',
'dicts',
'nested_data_structures',
'if',
'while',
'for',
'functions',
'files',
'csv_files',
'csv_file_with_header',
'type_conversion',
)
my_dir_path = os.path.dirname(__file__)
if len(my_dir_path) == 0:
my_dir_path = os.getcwd()
for file_name in os.listdir(my_dir_path):
if os.path.splitext(file_name)[1] != '.py':
continue
elif file_name[0] == '_' or file_name[0] == '.':
continue
file_path = os.path.join(my_dir_path, file_name)
file_name_split = file_name.split('_', 1)
if len(file_name_split) == 1:
continue
try:
file_number = int(file_name_split[0])
except ValueError:
continue
new_file_path = os.path.join(my_dir_path, file_name_split[1])
print file_path, 'to', new_file_path
os.rename(file_path, new_file_path)
for file_i, file_base_name in enumerate(order):
file_path = os.path.join(my_dir_path, file_base_name + '.py')
assert os.path.exists(file_path), file_path
new_file_path = os.path.join(my_dir_path, "%02u_%s.py" % (file_i+1, file_base_name))
print file_path, 'to', new_file_path
os.rename(file_path, new_file_path)