Python
提供:kuhalaboWiki
(版間での差分)
(→画像処理) |
(→CGI) |
||
| (1人の利用者による、間の3版が非表示) | |||
| 1行: | 1行: | ||
| + | |||
| + | == CGI == | ||
| + | https://techacademy.jp/magazine/21488 | ||
| + | |||
| + | ローカルhttpサーバーをpythonで立ち上げる。 | ||
| + | <pre> | ||
| + | python3 -m http.server --cgi 8000 | ||
| + | </pre> | ||
| + | |||
| + | <pre> | ||
| + | python -m SimpleHTTPServer | ||
| + | </pre> | ||
| + | |||
== 画像処理 == | == 画像処理 == | ||
| 18行: | 31行: | ||
import glob | import glob | ||
| − | + | file_list = glob.glob('path/to/dir/*.jpg') | |
| − | + | for filename in file_list: | |
| − | + | with open(filename, 'r') as input: | |
| − | + | ... | |
| − | + | ||
| − | + | ||
</pre> | </pre> | ||
| 32行: | 43行: | ||
import glob | import glob | ||
| − | + | file_list = sorted(glob.glob('path/to/dir/*.txt')) | |
| − | + | ||
| − | + | ||
| − | + | for filename in file_list: | |
| − | + | with open(filename, 'r') as input: | |
| − | + | ... | |
</pre> | </pre> | ||
2020年12月9日 (水) 02:48時点における最新版
目次 |
[編集] CGI
https://techacademy.jp/magazine/21488
ローカルhttpサーバーをpythonで立ち上げる。
python3 -m http.server --cgi 8000
python -m SimpleHTTPServer
[編集] 画像処理
[編集] アルファブレンド
- openCV
- NumPy
[編集] ファイル操作
[編集] ディレクトリー内のファイルの読み込み
- glob
ディレクトリー内のファイルのリストを取得
import glob
file_list = glob.glob('path/to/dir/*.jpg')
for filename in file_list:
with open(filename, 'r') as input:
...
ソートして読み込む。
import glob
file_list = sorted(glob.glob('path/to/dir/*.txt'))
for filename in file_list:
with open(filename, 'r') as input:
...
元のリストをランダムソート(シャッフル): random.shuffle()
import random l = list(range(5)) print(l) # [0, 1, 2, 3, 4] random.shuffle(l) print(l) # [4, 3, 2, 1, 0]
ディレクトリー内のファイルのリストを取得し、ランダムにシャッフルする。
import glob
import random
if __name__ == '__main__':
file_list = glob.glob('path/to/dir/*.jpg')
random.shuffle(file_list)
for filename in file_list:
with open(filename, 'r') as input:
...
リストからランダムに要素を抽出する。
import random
l = [0, 1, 2, 3, 4]
print(random.choice(l))
# 0
print(random.choice(('xxx', 'yyy', 'zzz')))
# yyy
ランダムに複数の要素を選択(重複なし): random.sample() randomモジュールの関数sample()で、リストからランダムで複数の要素を取得できる。要素の重複はなし(非復元抽出)。
第一引数にリスト、第二引数に取得したい要素の個数を指定する。リストが返される。
import random l = [0, 1, 2, 3, 4] print(random.sample(l, 3)) # [1, 3, 2] print(type(random.sample(l, 3))) # <class 'list'>
[編集] ネットワーク
[編集] SCP転送
- paramikoを使う
- paramikoインストール
pip install paramiko
- ssh接続
import paramiko
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='XXX.XXX.XXX.XXX', port=22, username='username', password='password')
- scp client作成
import scp
with scp.SCPclient(ssh.get_transport()) as scp:
scp.put('filename', '/upload/to/remote/directory/')
scp.get('/upload/to/remote/directory/')
- 上記の2つをまとめる。
import paramiko
import scp
with paramiko.SSHClient() as ssh:
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='XXX.XXX.XXX.XX', port=22, username='username', password='password')
with scp.SCPclient(ssh.get_transport()) as scp:
scp.put('filename', '/upload/to/remote/directory/')
scp.get('/upload/to/remote/directory/')