while文

言語リファレンス

while 文

永久ループ

入力が完了するまで繰り返すような場合に使用する。

while True:

if文で条件によってbreakを実行してループを抜けるようにする。

    if (answer == 'y'): break

break/pass/continue

チュートリアル

More Control Flow Tools

break

forやwhileの一番内側のループから抜け出す。

pass

文を書くことが構文上要求されているが、プログラム上何の動作もする必要がない時に使われる。

continue

以降のステートメントをスキップして、ループの先頭に戻って次のイテレーションを実行する。

osモジュール

osに依存するオペレーション

標準ライブラリー

os — Miscellaneous operating system interfaces

ファイルリストの作成

ディレクトリーにあるファイルのリストを作成する。

directory1 = 'xxxxxxxx'
list_files = os.listdir(path=directory1)

ディレクトリーの作成

os.makedirs(new_dir, exist_ok=True)

“exist_ok=True”の場合は、存在していてもFileExistsErrorにならない。

PWDの設定

“tkinter.filedialog.askdirectory()”で取得したディレクトリーに移動する。

directory = tkinter.filedialog.askdirectory(
        title='Enter Work Directory',
        initialdir=work_directory,
        mustexist=True
        )
os.chdir(directory)

PWDの取得

current_dir = os.getcwd()

chmod(パーミッションの変更)

os.chmod(filename, stat.S_IWRITE)
  • S_IWRITE:オーナーがWrite権限を持つ。
  • S_IREAD:オーナーがRead権限を持つ。
  • S_IEXEC:オーナーが実行権限を持つ。

ファイルの削除

os.remove(filename)

ファイルであることの確認

strで指定したファイルパスがファいおるである場合に、BooleanのTrueを返す。

file_path = './public/temp1'
if (os.path.isfile(file_path) == True):

shutilモジュール

ファイルやファイルの集まりに対する高度な操作を行う。

標準ライブラリー

shutil — 高水準のファイル操作

インポート

import shutil

ファイルのコピー

ファイルのデータパーミッションをコピーする。

ファイル名はPath Like Objectか文字列でなければならない。

shutil.copy(source_file_name, target_file_name)

ファイルのメタデータも一緒にコピーする。

shutil.copy2(source_file_name, target_file_name)

boto3モジュール

AWSのサービスをPyhtonからコントロールするためのモジュール(AWS SDK for Python)

AWSドキュメント

AWS SDK for Python (Boto3)

インストール

py -m pip install boto3

インポート

import boto3

インスタンスのスタート・ストップ

client = boto3.client(\
        'ec2',\
        aws_access_key_id='xxxxxxxx',\
        aws_secret_access_key='xxxxxxxx',\
        region_name='ap-northeast-1'\
        )

# Start EC2
response = client.start_instances(InstanceIds=['xxxxxxxx',],)

# Stop EC2
response = client.stop_instances(InstanceIds=['xxxxxxxx',],)

# Start RDS
response = client.start_db_instance(DBInstanceIdentifier='xxxxxxxx')

# Stop RDS
response = client.stop_db_instance(DBInstanceIdentifier='xxxxxxxx')

Athena Create/Drop Database

client = boto3.client(\
        'athena',\
        aws_access_key_id='xxxxxxxx',\
        aws_secret_access_key='xxxxxxxx',\
        region_name='ap-northeast-1'\
        )

response = client.start_query_execution(
        QueryString=f'{'crteate'} database {'xxxxxxxx'}',
        ResultConfiguration={"OutputLocation": s3_query_result}
    )

Athena Create Table

client = boto3.client(\
        'athena',\
        aws_access_key_id='xxxxxxxx',\
        aws_secret_access_key='xxxxxxxx',\
        region_name='ap-northeast-1'\
        )

query1 = file1.read()    # Read Athena Table Definition Codes

response = client.start_query_execution(\
                    QueryString=query1,\
                    ResultConfiguration={"OutputLocation": s3_query_result}
                )

QueryStringは複数行になるので、Athenaのテーブル定義のステートメントをファイルから読み込んだ変数を指定する。