0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

動作しているプログラムがあるディレクトリ名を取得する

Posted at

vscodeでプロジェクト全体のディレクトリを開いている時に内包する個別プロジェクトのプログラムを動作させたとき、相対パスで出力を書いていると全体のディレクトリに漏れ出す

foo.py
## global/local/foo.pyのプログラムを端末のpwdがglobalのところで実行したとする
## 例 `python3 blobal/local/foo.py

# こう書くと実行時のpwdに漏れ出す
with open("bar.txt", "w") as bar:
    bar.write("hello")

# 変更する
dir = os.path.dirname(__file__)
with open(os.path.join(dir, "bar.txt") as bar:
    bar.write("hello")

0
2
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
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?