1
0

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 3 years have passed since last update.

【Python/Pygame】pygame.image.loadにおける、読み込みデータの相対パス指定方法について

Posted at

なんでやねん

筆者のPython関係の環境は下記の通り。

OS: Windows10 Home Edittion
IDE: Visual Studio Code ver 1.55.2
Python ver: 3.9.1
pygame ver: 2.0.1

pythonコードと同一フォルダ内に「img」フォルダを置き、その中に「bg.png」を入れ
その画像ファイルを読み込ませるため相対パスを下記の通り記述した。

bg = pygame.image.load("../img/bg.png")

…のだが、どうにも読み込んでくれない。
VSC先生のコンソールより
「FileNotFoundError: No such file or directory.」とお叱りの言葉を下されるばかり。

親ディレクトリまでパス指定しよう

調べてみると、pythonファイルの入っている親ディレクトリまで指定しないといけない模様。
Documents内のTestディレクトリ内にpythonコードを入れているので、
正しくは

bg = pygame.image.load("../Documents/Test/img/bg.png")

とすべきだったらしい。
上記のようなパス指定で無事画像ファイルは読み込めた。

1
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?