言語リファレンス
永久ループ
入力が完了するまで繰り返すような場合に使用する。
while True:
if文で条件によってbreakを実行してループを抜けるようにする。
if (answer == 'y'): break
Since 2023/09/17
入力が完了するまで繰り返すような場合に使用する。
while True:
if文で条件によってbreakを実行してループを抜けるようにする。
if (answer == 'y'): break
forやwhileの一番内側のループから抜け出す。
文を書くことが構文上要求されているが、プログラム上何の動作もする必要がない時に使われる。
以降のステートメントをスキップして、ループの先頭に戻って次のイテレーションを実行する。
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にならない。
“tkinter.filedialog.askdirectory()”で取得したディレクトリーに移動する。
directory = tkinter.filedialog.askdirectory(
title='Enter Work Directory',
initialdir=work_directory,
mustexist=True
)
os.chdir(directory)
current_dir = os.getcwd()
os.chmod(filename, stat.S_IWRITE)
os.remove(filename)
strで指定したファイルパスがファいおるである場合に、BooleanのTrueを返す。
file_path = './public/temp1' if (os.path.isfile(file_path) == True):
ファイルやファイルの集まりに対する高度な操作を行う。
import shutil
ファイルのデータとパーミッションをコピーする。
ファイル名はPath Like Objectか文字列でなければならない。
shutil.copy(source_file_name, target_file_name)
ファイルのメタデータも一緒にコピーする。
shutil.copy2(source_file_name, target_file_name)
AWSのサービスをPyhtonからコントロールするためのモジュール(AWS SDK for Python)
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')
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}
)
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のテーブル定義のステートメントをファイルから読み込んだ変数を指定する。