import os
import os.path
import nbconvert
import glob
检查路径是否存在,删除旧的文件:
if not os.path.exists('static-files'):
os.mkdir('static-files')
for n in glob.glob('static-files/*/*/*'):
os.remove(n)
文件夹:
folders = ['01-python-tools',
'02-python-essentials',
'03-numpy',
'04-scipy',
'05-advanced-python',
'06-matplotlib',
'07-interfacing-with-other-languages',
'08-object-oriented-programming',
'09-theano',
'10-something-interesting',
'11-useful-tools',
'12-pandas'
]
遍历文件夹得到所有的文件名:
file_names = []
for folder in folders:
files = sorted(os.listdir(folder))
file_names += [os.path.join(folder, file_name) for file_name in files if file_name.endswith('.ipynb')]
def convert_to_files(names, to_format):
target_dir = os.path.join("static-files", to_format)
for folder in folders:
if not os.path.exists(os.path.join(target_dir, folder)):
os.makedirs(os.path.join(target_dir, folder))
for file_name in names:
p = nbconvert.export(nbconvert.get_exporter(to_format), file_name)
with open(os.path.join(target_dir, file_name[:-6] + p[1]["output_extension"]), 'w') as f:
f.write(p[0].encode("utf-8"))
print file_name
转化 HTML 文件:
convert_to_files(file_names, "html")
01-python-tools/01.01-python-overview.ipynb 01-python-tools/01.02-ipython-interpreter.ipynb 01-python-tools/01.03-ipython-notebook.ipynb 01-python-tools/01.04-use-anaconda.ipynb 02-python-essentials/02.01-a-tour-of-python.ipynb 02-python-essentials/02.02-python-data-types.ipynb 02-python-essentials/02.03-numbers.ipynb 02-python-essentials/02.04-strings.ipynb 02-python-essentials/02.05-indexing-and-slicing.ipynb 02-python-essentials/02.06-lists.ipynb 02-python-essentials/02.07-mutable-and-immutable-data-types.ipynb 02-python-essentials/02.08-tuples.ipynb 02-python-essentials/02.09-speed-comparison-between-list-&-tuple.ipynb 02-python-essentials/02.10-dictionaries.ipynb 02-python-essentials/02.11-sets.ipynb 02-python-essentials/02.12-frozen-sets.ipynb 02-python-essentials/02.13-how-python-assignment-works.ipynb 02-python-essentials/02.14-if-statement.ipynb 02-python-essentials/02.15-loops.ipynb 02-python-essentials/02.16-list-comprehension.ipynb 02-python-essentials/02.17-functions.ipynb 02-python-essentials/02.18-modules-and-packages.ipynb 02-python-essentials/02.19-exceptions.ipynb 02-python-essentials/02.20-warnings.ipynb 02-python-essentials/02.21-file-IO.ipynb 03-numpy/03.01-numpy-overview.ipynb 03-numpy/03.02-matplotlib-basics.ipynb 03-numpy/03.03-numpy-arrays.ipynb 03-numpy/03.04-array-types.ipynb 03-numpy/03.05-array-calculation-method.ipynb 03-numpy/03.06-sorting-numpy-arrays.ipynb 03-numpy/03.07-array-shapes.ipynb 03-numpy/03.08-diagonals.ipynb 03-numpy/03.09-data-to-&-from-string.ipynb 03-numpy/03.10-array-attribute-&-method-overview-.ipynb 03-numpy/03.11-array-creation-functions.ipynb 03-numpy/03.12-matrix-object.ipynb 03-numpy/03.13-general-functions.ipynb 03-numpy/03.14-vectorizing-functions.ipynb 03-numpy/03.15-binary-operators.ipynb 03-numpy/03.16-universal-functions.ipynb 03-numpy/03.17-choose.ipynb 03-numpy/03.18-array-broadcasting.ipynb 03-numpy/03.19-reading-and-writing-arrays.ipynb 03-numpy/03.20-structured-arrays.ipynb 03-numpy/03.21-record-arrays.ipynb 03-numpy/03.22-memory-maps.ipynb 03-numpy/03.23-from-matlab-to-numpy.ipynb 04-scipy/04.01-scienticfic-python-overview.ipynb 04-scipy/04.02-interpolation-with-scipy.ipynb 04-scipy/04.03-statistics-with-scipy.ipynb 04-scipy/04.04-curve-fitting.ipynb 04-scipy/04.05-minimization-in-python.ipynb 04-scipy/04.06-integration-in-python.ipynb 04-scipy/04.07-ODEs.ipynb 04-scipy/04.08-sparse-matrix.ipynb 04-scipy/04.09-linear-algbra.ipynb 04-scipy/04.10-sparse-linear-algebra.ipynb 05-advanced-python/05.01-overview-of-the-sys-module.ipynb 05-advanced-python/05.02-interacting-with-the-OS---os.ipynb 05-advanced-python/05.03-comma-separated-values.ipynb 05-advanced-python/05.04-regular-expression.ipynb 05-advanced-python/05.05-datetime.ipynb 05-advanced-python/05.06-sql-databases.ipynb 05-advanced-python/05.07-object-relational-mappers.ipynb 05-advanced-python/05.08-functions.ipynb 05-advanced-python/05.09-iterators.ipynb 05-advanced-python/05.10-generators.ipynb 05-advanced-python/05.11-context-managers-and-the-with-statement.ipynb 05-advanced-python/05.12-decorators.ipynb 05-advanced-python/05.13-decorator-usage.ipynb 05-advanced-python/05.14-the-operator-functools-itertools-toolz-fn-funcy-module.ipynb 05-advanced-python/05.15-scope.ipynb 05-advanced-python/05.16-dynamic-code-execution.ipynb 06-matplotlib/06.01-pyplot-tutorial.ipynb 06-matplotlib/06.02-customizing-plots-with-style-sheets.ipynb 06-matplotlib/06.03-working-with-text---basic.ipynb 06-matplotlib/06.04-working-with-text---math-expression.ipynb 06-matplotlib/06.05-image-tutorial.ipynb 06-matplotlib/06.06-annotating-axes.ipynb 06-matplotlib/06.07-legend.ipynb 06-matplotlib/06.08-figures,-subplots,-axes-and-ticks.ipynb 06-matplotlib/06.09-do-not-trust-the-defaults.ipynb 06-matplotlib/06.10-different-plots.ipynb 07-interfacing-with-other-languages/07.01-introduction.ipynb 07-interfacing-with-other-languages/07.02-python-extension-modules.ipynb 07-interfacing-with-other-languages/07.03-cython-part-1.ipynb 07-interfacing-with-other-languages/07.04-cython-part-2.ipynb 07-interfacing-with-other-languages/07.05-cython-part-3.ipynb 07-interfacing-with-other-languages/07.06-cython-part-4.ipynb 07-interfacing-with-other-languages/07.07-profiling-with-annotations.ipynb 07-interfacing-with-other-languages/07.08-ctypes.ipynb 08-object-oriented-programming/08.01-oop-introduction.ipynb 08-object-oriented-programming/08.02-using-oop-model-a-forest-fire.ipynb 08-object-oriented-programming/08.03-what-is-a-object.ipynb 08-object-oriented-programming/08.04-writing-classes.ipynb 08-object-oriented-programming/08.05-special-method.ipynb 08-object-oriented-programming/08.06-properties.ipynb 08-object-oriented-programming/08.07-forest-fire-simulation.ipynb 08-object-oriented-programming/08.08-inheritance.ipynb 08-object-oriented-programming/08.09-super.ipynb 08-object-oriented-programming/08.10-refactoring-the-forest-fire-simutation.ipynb 08-object-oriented-programming/08.11-interfaces.ipynb 08-object-oriented-programming/08.12-public-private-special-in-python.ipynb 08-object-oriented-programming/08.13-multiple-inheritance.ipynb 09-theano/09.01-introduction-and-installation.ipynb 09-theano/09.02-theano-basics.ipynb 09-theano/09.03-gpu-on-windows.ipynb 09-theano/09.04-graph-structures.ipynb 09-theano/09.05-configuration-settings-and-compiling-modes.ipynb 09-theano/09.06-conditions-in-theano.ipynb 09-theano/09.07-loop-with-scan.ipynb 09-theano/09.08-linear-regression.ipynb 09-theano/09.09-logistic-regression-.ipynb 09-theano/09.10-softmax-on-mnist.ipynb 09-theano/09.11-net-on-mnist.ipynb 09-theano/09.12-random-streams.ipynb 09-theano/09.13-modern-net-on-mnist.ipynb 09-theano/09.14-convolutional-net-on-mnist.ipynb 09-theano/09.15-tensor-basics.ipynb 09-theano/09.16-tensor-indexing.ipynb 09-theano/09.17-tensor-operator-and-elementwise-operations.ipynb 09-theano/09.18-tensor-nnet-.ipynb 09-theano/09.19-tensor-conv.ipynb 10-something-interesting/10.01-maps-using-basemap.ipynb 10-something-interesting/10.02-maps-using-cartopy.ipynb 10-something-interesting/10.03-nba-data.ipynb 10-something-interesting/10.04-louis-cha's-kungfu-world.ipynb 11-useful-tools/11.01-pprint.ipynb 11-useful-tools/11.02-pickle-and-cPickle.ipynb 11-useful-tools/11.03-json.ipynb 11-useful-tools/11.04-glob.ipynb 11-useful-tools/11.05-shutil.ipynb 11-useful-tools/11.06-gzip,-zipfile,-tarfile.ipynb 11-useful-tools/11.07-logging.ipynb 11-useful-tools/11.08-string.ipynb 11-useful-tools/11.09-collections.ipynb 11-useful-tools/11.10-requests.ipynb 12-pandas/12.01-ten-minutes-to-pandas.ipynb 12-pandas/12.02-series-in-pandas.ipynb 12-pandas/12.03-dataframe-in-pandas.ipynb
产生新目录:
with open('index.md') as f:
text = f.read()
with open(os.path.join("static-files", "html", "README.md"), "w") as g:
g.write(text.replace(".ipynb", ".html"))