質問編集履歴
3
お礼追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -54,7 +54,7 @@
|
|
54
54
|
よろしくお願いしますm(_)m
|
55
55
|
|
56
56
|
# できました! ありがとうございます(≧∀≦)
|
57
|
-
|
57
|
+
[umyu](https://teratail.com/users/umyu)様、ありがとうございますヽ(゚▽゚*)乂(*゚▽゚)ノ
|
58
58
|
```python:extract.py
|
59
59
|
# extract.py
|
60
60
|
# -*- coding:utf-8 -*-
|
2
完成したコードを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -51,4 +51,36 @@
|
|
51
51
|
- [Python 外部ファイルで定義した変数を使用したい](https://teratail.com/questions/51381) を参考にfilelistname変数とindex変数をglobal で定義。
|
52
52
|
- 上記変更後、extract.pyを保存。
|
53
53
|
|
54
|
-
よろしくお願いしますm(_)m
|
54
|
+
よろしくお願いしますm(_)m
|
55
|
+
|
56
|
+
# できました! ありがとうございます(≧∀≦)
|
57
|
+
|
58
|
+
```python:extract.py
|
59
|
+
# extract.py
|
60
|
+
# -*- coding:utf-8 -*-
|
61
|
+
|
62
|
+
import glob
|
63
|
+
import os
|
64
|
+
|
65
|
+
filelistname = ""
|
66
|
+
index = 0
|
67
|
+
def extract_files(path, ext):
|
68
|
+
for file_name in sorted(glob.glob(path + "/*" + ext)):
|
69
|
+
print(file_name) # ./codes/page1_script.js
|
70
|
+
yield file_name
|
71
|
+
```
|
72
|
+
|
73
|
+
```python:javascript_kindle.py
|
74
|
+
# javascript_kindle.py
|
75
|
+
# -*- coding:utf-8 -*-
|
76
|
+
|
77
|
+
import os
|
78
|
+
import extract
|
79
|
+
files = os.listdir('./codes')
|
80
|
+
filenum = len(files) + 1
|
81
|
+
|
82
|
+
with open('./output.txt', 'w+') as f:
|
83
|
+
for file_name in extract.extract_files("./codes", ".js"):
|
84
|
+
with open(file_name, 'r') as file_data:
|
85
|
+
f.write('/' * 24 + '\n//' + os.path.basename(file_name) + '\n' + file_data.read() + '\n')
|
86
|
+
```
|
1
画像追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,6 +6,7 @@
|
|
6
6
|
# 困っていること
|
7
7
|
- 同じ階層にあるextract.pyをjavascript_kindle.pyにimportしても、そのモジュール内の変数 (filelistnameとindex) がなぜか片方 (index) 使えない。
|
8
8
|
- importしたモジュール内の変数indexを使う行で `Undefined valuable "index"`と出る (赤い×印の行)
|
9
|
+

|
9
10
|

|
10
11
|
|
11
12
|
```python:extract.py
|