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):