MEMO
- subprocess.Popen で指定するPATHは、pathlibのパスはだめ!
目次
基本
# フォルダを開く
import subprocess
def open_folder(dir_p):
subprocess.Popen(["explorer", dir_p], shell=True)
popen.wait() # ここは不要?
# Macの場合> subprocess.call(['open', dir_p])
# WEBを開く(Chromeで開く場合は違うかも)
import subprocess
import webbrowser
def open_url(url):
webbrowser.open("url")作業フォルダをつくる→ファイルをコピー→フォルダを開く
from datetime import datetime
import pathlib
import shutil
import subprocess
# home()でユーザープロファイルまで出せる
curent = pathlib.Path.home() / 'Desktop'
# 作業フォルダを作成
def make_work_dir():
# そのままENTERした場合、ファイル名は「tmp」
dirname = input('dirname?: ')
# or fixed name
if dirname == '':
dirname = 'tmp'
suffix_now = (datetime.now()).strftime('%Y%m')
dirname = f'{dirname}_{suffix_now}'
global dir_p
dir_p = curent / dirname
if dir_p.exists():
if input('WARNING: Overwrite same folder?(y/n): ') == 'y':
if input('Realy? Realy? Overwrite?(y/n): ') == 'y':
shutil.rmtree(dir_p)
dir_p.mkdir()
print(f'Done: {dir_p}')
else:
print('Canceled')
return
else:
print('Canceled')
return
else:
dir_p.mkdir()
print(f'Done: {dir_p}')
# コピーするファイルFMT(ex.いつも使うフォーマット等)
def copy_fmt_file_to_dir(src_file):
dst_file = dir_p / src_file.name
if src_file.exists():
shutil.copy2(src_file, dst_file)
else:
print(f'処理を中断します。コピー元ファイルがありません。')
print(f'{src_file=}')
# フォルダを自動で開く
def open_work_dir(dir_p):
# Win
subprocess.Popen(["explorer", dir_p], shell=True)
popen.wait()
# ================================================================
# 作業フォルダをつくる -> 前回ファイルをコピーする -> 作業フォルダを開く
# ================================================================
# 作業フォルダを作成
make_work_dir()
# 作業用ファイルFMTをコピー
src_file = pathlib.Path('/Users/mbp441/Desktop/nofu_sample.csv')
copy_fmt_file_to_dir(src_file)
# 作業フォルダを開く
open_work_dir(dir_p)
# EOF