開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Python 3.5 (プログラミング言語)
Exercises for Programmers: 57 Challenges to Develop Your Coding Skills (Brian P. Hogan 著、Pragmatic Bookshelf)のChapter 8(Working with Files)、43(Website Generator)を取り組んでみる。
43(Website Generator)
コード(Emacs)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
name = input('Site name: ')
author = input('Author: ')
js = input('Do you want a folder for JavaScript? ')
css = input('Do you want a folder for CSS? ')
os.mkdir(name)
style = ''
if js == 'y':
os.mkdir(os.path.join(name, 'js'))
if css == 'y':
os.mkdir(os.path.join(name, 'css'))
style = (
'\n <link rel="stylesheet" type="text/css" href="{0}/">'
.format('css')
)
with open(os.path.join(name, 'index.html'), 'w') as f:
print('''<!doctype html>
<html>
<head>
<meta name="author" content="{author}">
<title>{title}</title>{style}
</head>
<body>
</body>
</html>'''.format(author=author, title=name, style=style),
file=f)
print('Created {0}\n'
'Created {1}'.format(os.path.join(os.curdir, name), 'index.html'))
if js == 'y':
print('Created {0}/'.format(os.path.join(os.curdir, name, 'js')))
if css == 'y':
print('Created {0}/'.format(os.path.join(os.curdir, name, 'css')))
入出力結果(Terminal, IPython)
$ ./sample43.py
Site name: awesomeco
Author: Max Power
Do you want a folder for JavaScript? y
Do you want a folder for CSS? y
Created ./awesomeco
Created index.html
Created ./awesomeco/js/
Created ./awesomeco/css/
$ ls -R awesomeco/
./ ../ css/ index.html js/
awesomeco//css:
./ ../
awesomeco//js:
./ ../
$ cat awesomeco/index.html
<!doctype html>
<html>
<head>
<meta name="author" content="Max Power">
<title>awesomeco</title>
<link rel="stylesheet" type="text/css" href="css/">
</head>
<body>
</body>
</html>
$ ./sample43.py
Site name: python
Author: kamimura
Do you want a folder for JavaScript? n
Do you want a folder for CSS? n
Created ./python
Created index.html
$ ls -R python/
./ ../ index.html
$ cat python/index.html
<!doctype html>
<html>
<head>
<meta name="author" content="kamimura">
<title>python</title>
</head>
<body>
</body>
</html>
$
0 コメント:
コメントを投稿