LoginSignup
0
1

More than 1 year has passed since last update.

Python逆引き小辞典

Last updated at Posted at 2023-04-13

はじめに

Pythonでこれやるにはどう書くんだっけ?ということをちまちままとめます。
そのうち大辞典になる予定です。

目次

  1. ファイル操作
    1-1.ファイルの存在確認
    1-2.ディレクトリの存在確認
  2. テキスト操作
    2-1.末尾の文字を削除
  3. pythonプログラムのexe化(#Chapter4)

ファイルの存在確認

chap1.py
import os

path = 'file.txt'
is_file_exist = os.path.isfile(path)

if is_file_exist:
    # ファイルは存在する
    print(f"{path} is a file.")
else:
    # パスが存在しないかファイルではない
    print(f"{path} is not exist.")

ディレクトリの存在確認

chap2.py
import os

path = 'dir_name'
is_dir_exist = os.path.isdir(path)

if is_dir_exist:
    # ディレクトリは存在する
    print(f"{path} is a directory.")
else:
    # パスが存在しないかディレクトリではない
    print(f"{path} is not a directory.") 

末尾の文字を削除

chap3.py
string = '1234567890'

str = string.rstrip('0')
print(str)

pythonプログラムのexe化

[pycharm]
File→Setting→Python Interpreter→+ボタンを押す→pyinstaller→install package
pycharmの左下でterminalタブを開き
PS C:\Users\xxx\PycharmProjects\xxx > pyinstaller main.py --onefile

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1