forked from halfrost/LeetCode-Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetFile.py
More file actions
56 lines (44 loc) · 1.6 KB
/
Copy pathGetFile.py
File metadata and controls
56 lines (44 loc) · 1.6 KB
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
48
49
50
51
52
53
54
55
56
import os
from os.path import join
from shutil import move
toc_string = """\n
---
bookToc: false
---
\n"""
pre_string = """
## 代码
```go
"""
end_string = "\n```"
codeContent = ""
current_working_dir = os.getcwd()
# print(f"current_working_dir: {current_working_dir}")
# dir_names = os.listdir(current_working_dir)
# 重命名目录。 os.rename(dir_name, new_dir_name)
# 遍历目录
# dir_path是当前遍历到的目录。dir_names是dir_path下的文件夹列表。file_names是是dir_path下的文件列表
# 如果想实现目录白名单,将白名单目录从dir_names中去除即可
for (dir_path, dir_names, file_names) in os.walk(current_working_dir):
# print(f"当前遍历到的目录: {dir_path}")
os.chdir(dir_path)
files = dir_path.split("/")
new_file_name = files[len(files) - 1] + '.md'
print(f"当前所在的文件夹: {os.getcwd()}")
for file_name in file_names:
if(file_name.endswith('.go') and not file_name.endswith('_test.go')):
print(f"当前所在文件: {file_name}")
with open(file_name, "r") as myfile:
codeContent = myfile.read()
break
# print(codeContent)
for file_name in file_names:
if(file_name.endswith('.md')):
print(f"当前所在文件: {file_name}")
with open(file_name, "a") as myfile:
myfile.write(pre_string)
myfile.write(codeContent)
myfile.write(end_string)
os.rename(file_name, new_file_name)
move(join(dir_path, new_file_name), current_working_dir)
break