Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pycharm can't read file
#1
Pycharm can't read a file (which is in the directory), and it gives me the error message "FileNotFoundError: No file 'images.png' found in working directory '/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/video /.venv'
this is the full code:
import pygame

pygame.init()
WINDOW_WIDTH, WINDOW_HEIGHT = 1280, 720
pygame.display.set_caption("Space Shooter")
display_surface = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
running = True

surf = pygame.Surface((100, 200))
surf.fill("orange")
x = 100

player_surface = pygame.image.load('images.png')


while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    display_surface.fill('darkgray')
    x += 0.1
    display_surface.blit(surf, (x, 150))
    pygame.display.update()
pygame.quit()
Yoriz write Jul-24-2024, 05:04 PM:
Please post all code, output and errors (in its entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Please use bb tags when posting code.

I usually get the path to the working script to load a file.

from pathlib import Path

path = Path(__file__).parent


image = pygame.image.load(f'{path}/my_img.png')
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags
Download my project scripts


Reply
#3
A few questions here.

Why is your virtual environment in your python distribution? You should not be adding folders to site packages, except through pip or some other package manager.

Why is your current working directory in you python distribution. You should not save the files you write in your python distribution.

Make a folder under your home folder. You should not be doing anything in /library... Put your python files in your new folder under home. Create your virtual environment in your new folder under home. When you start a new project, create a new folder, making it a practice to make a new folder and a new virtual environment, and keeping all your folders together in that folder and apart from your python distribution(s).
Reply
#4
Im extremely new at Python, so could you explain that in more simple terms, because I have no idea on what any on that means. Sorry.
Reply
#5
Thanks, I used this code and replaced (__file__) with the file path, and it worked perfectly
Reply
#6
You have not fixed your problem. __file__ should not be "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/video /.venv" as reported in the error messages because you should never be creating files at that location.

There should not be a .venv folder in "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/video". .venv is a folder created by PyCharm. It is a special version of Python called a "virtual environment" that pycharm created for running your program. A virtual environment protects the "system python" from getting messed up when you install packages or different versions of python. The .venv folder should never be under "/Library/Frameworks/Python.framework/Versions". It should be in a folder you created for doing your project work.

I suspect that "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/video" is also in the wrong place. A very quick scan of PyPI (python package index) did not reveal any common packages that create a "video" folder in the "site-packages" folder. I don't think this folder is a python package you installed on your computer. My guess is you created this folder for doing your python work. The problem is that you created the folder in the wrong place.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to read a file as binary or hex "string" so that I can do regex search? tatahuft 3 956 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,182 Sep-15-2024, 06:14 PM
Last Post: zinho
  Python is unable to read file Genericgamemaker 13 3,406 Jul-19-2024, 06:42 PM
Last Post: snippsat
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,092 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,485 Nov-26-2023, 07:51 AM
Last Post: Pedroski55
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,688 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 2,498 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 2,200 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 4,725 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 23,613 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020