指定フォルダ以下のファイルをコピーする - Python
指定のフォルダの下にあるファイルをコピー
再帰的に見ていく
再帰的にファイルを見るのにpathlib.Path.glob()
を使い、ファイルをコピーするのにshutil.copy()
を使う
例)C:\sample
以下のファイルで、ファイル名に"a"
が含まれているファイルをカレントディレクトリにコピーする
>>> from pathlib import Path >>> p = Path(r"C:\sample") WindowsPath('C:/sample') >>> for f in p.glob("**/*a*): ... shutil.copy(f.absolute(), "./")
参考文献
nonlocal ネストした関数から変数にアクセスする - Python
【Windows】フォルダをエクスプローラで開く - Python
subprocess
モジュールのcall
を使う
import subprocess subprocess.call(f'explorer "{開くフォルダのパス}"')
参考文献
os.path.expanduser関数 - Python
カレントユーザのホームディレクトリのパスを取得することができる
>>> os.path.expanduser('~/src/python/') '/Users/tamago324/src/python/'
使えるときあるのかな?
参考文献
【IFNULL関数】 AがNULLだったら、B。AがNULLじゃなかったら、Aを返す関数 - MySQL
便利なのを知ったからメモ
もし、第1パラメータがNULL
だったら、第2パラメータを返し、
第1パラメータがNULL
じゃなかったら、第2パラメータを返してくれる便利なやつ
第1パラメータがNULL
ではない時
SELECT IFNULL('AAA', 'BBB') FROM DUAL
結果
AAA
第1パラメータが返ってくる
第1パラメータがNULL
の時
SELECT IFNULL(NULL, 'BBB') FROM DUAL
結果
BBB
第2パラメータが返ってくる
Pythonでのor
みたいな?
>>> 'hoge' or 'fuga' 'hoge' >>> None or 'fuga' 'fuga'
参考文献
keybindingに渡されるeventという引数は何者!? - Python
keybindingのメソッドに渡されるevent
という引数は何者?
prompt_toolkit.key_binding.key_processor.KeyPressEvent
というやつ
- data
- key_processor
- app: The current
Application
object. - current_buffer: The current buffer.
- arg
- arg_present
- append_to_arg_count
- cli
@kb.add('a') def _(event): event.app.xxxxx
みたいにevent.appしておけばどうにかなる説
Application オブジェクトに全部格納してるから、それ見ればなんとかなるしょみたいな
ちょっと脱線
Applicaiton.current_buffer
はlayout.current_buffer
らしい?
@property def current_buffer(self): """ The currently focused :class:`~.Buffer`. (This returns a dummy :class:`.Buffer` when none of the actual buffers has the focus. In this case, it's really not practical to check for `None` values or catch exceptions every time.) """ return self.layout.current_buffer or Buffer(name='dummy-buffer') # Dummy buffer.
あれか、起動する時にapp = Application(layout=xxxx)
ってやったときのやつが取得できるってことか!!
また、特定のウィンドウにフォーカスがあうかどうかとかはhas_focus
を使う