Python Forum
Python is unable to read file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python is unable to read file
#1
So I'm pretty new to coding and I've spent the past two days on a tutorial for making a simple game using pygame because of multiple issues I have had. This issue is listed as "SyntaxError: Non-UTF-8 code starting with '\xff' in file /Users/Itookoutthename/Library/Application Support/JetBrains/PyCharmCE2024.1/light-edit/pexels-photo-207529.jpeg on line 1, but no encoding declared; see https://peps.python.org/pep-0263/ for details" I've been looking online for hours and have no idea how to fix this. Any suggestions?
Reply
#2
jpeg is a binary format, so decoding does not happen when you read a jpeg file. You must be trying to read the file like it is a text file. I cannot think of a reason why you would do this, so the error is likely a logic error. I cannot offer suggestions on how to fix a logic error without seeing the errant code. You need to post the code that results in this error.
Reply
#3
Ok, this is the full code

import pygame
import time
import random



WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Asteroid Dodge')

BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2023.1/light-edit/Unkown.jpeg")

def main():
    run = True

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

    pygame.quit()
deanhystad write Jul-19-2024, 03:02 AM:
Please post all code, output and errors (it it's 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
#4
As the code stands now, it will not run.
Images will need to use win.blit to get the background image to show.
I do not see where you called the main function either.

Here is a basic example
import pygame
import os 

# Set a path to current directory of executing script
path = os.path.realpath(os.path.dirname(__file__))

# Pulling image from current directory
# This could be some other path
bg_image = pygame.image.load(f'{path}/my_img.png')

# Set the screen
screen = pygame.display.set_mode((800,600))

running = True

while running:
    # Blit the image to the screen surface
    screen.blit(bg_image, (0,0))

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

    # Update the display
    pygame.display.update()
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
#5
Like I said, I'm currently following a tutorial, so the code will not be complete. All I'm trying to figure out is how to fix the error I mentioned in the first post.
Reply
#6
Couple of questions. Does it run if you do this:
import pygame
import time
import random


WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Asteroid Dodge")

# BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2023.1/light-edit/Unkown.jpeg")


def main():
    run = True

    while run:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False  # = for assignment, not == for equality test
                break

    pygame.quit()


main()
You will get a window with a black background with the title Ateroid Dodge.

This confuses me:
BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2023.1/light-edit/Unkown.jpeg")
The file path looks like a file in pycharm. Is this a file you created? You should not put your source and support files in pycharm.
Reply
#7
BG = pygame.image.load("/Library/Application Support/JetBrains/PycharmCE2024.1/light-edit/Unkown.jpeg") is me trying to set the background to an image, though I haven't actually put that in the code (setting the background to BG). I'm just trying to get the code to read the file but I keep receiving the error message that I listed in the first post. And yes the code you included does run.
Reply
#8
(Jul-18-2024, 08:06 PM)Genericgamemaker Wrote: code starting with '\xff' in file /Users/Itookoutthename/Library/Application Support/JetBrains/PyCharmCE2024.1/light-edit/pexels-photo-207529.jpeg on line 1
You most use whole path on Windows,also with drive name, /Users/ is not correct.
Eg it should be like this C/:Users/<your username>/Itookoutthename/......

Also make you own folder,so avoid these long paths 🚀
Here is working example,see to ways to find files that works r(raw string) or just backslash this way /)
Also i made folder py_game with a img folder.
# space.py
import pygame
import time
import random

pygame.init()
WIDTH, HEIGHT = 1000, 800
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Asteroid Dodge')

# Load background image,Windows need whole parth with drive
BG = pygame.image.load(r"G:\div_code\py_game\img\Space.jpg")

# Load spaceship image
SPACESHIP = pygame.image.load("G:/div_code/py_game/img/spaceship.png")
SPACESHIP = pygame.transform.scale(SPACESHIP, (70, 70))

class Spaceship:
    def __init__(self):
        self.img = SPACESHIP
        self.x = WIDTH // 2
        self.y = HEIGHT - 400
        self.vel = 5

    def draw(self, window):
        window.blit(self.img, (self.x, self.y))

    def move(self, keys):
        if keys[pygame.K_LEFT] and self.x - self.vel > 0:
            self.x -= self.vel
        if keys[pygame.K_RIGHT] and self.x + self.vel + self.img.get_width() < WIDTH:
            self.x += self.vel

def main():
    run = True
    clock = pygame.time.Clock()
    spaceship = Spaceship()
    while run:
        clock.tick(60)
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
        keys = pygame.key.get_pressed()
        spaceship.move(keys)
        WIN.blit(BG, (0, 0))
        spaceship.draw(WIN)
        pygame.display.update()
    pygame.quit()

if __name__ == "__main__":
    main()
[Image: wzcr2Z.png]
Reply
#9
If I don't put my files in Pycharm, where should I put them?
Reply
#10
(Jul-19-2024, 05:05 PM)Genericgamemaker Wrote: If I don't put my files in Pycharm, where should I put them?
You should to learn some basic stuff first,this is needed when doing programming.
Create a New Folder on Windows
So do it both ways as practice with File Explorer and cmd, learn command line stuff using cmd.
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 1,138 Dec-19-2024, 11:57 AM
Last Post: snippsat
  Read TXT file in Pandas and save to Parquet zinho 2 1,270 Sep-15-2024, 06:14 PM
Last Post: zinho
  Pycharm can't read file Genericgamemaker 5 1,599 Jul-24-2024, 08:10 PM
Last Post: deanhystad
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 3,329 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Recommended way to read/create PDF file? Winfried 3 4,816 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,881 Nov-09-2023, 10:56 AM
Last Post: mg24
  read file txt on my pc to telegram bot api Tupa 0 2,633 Jul-06-2023, 01:52 AM
Last Post: Tupa
  parse/read from file seperated by dots giovanne 5 2,256 Jun-26-2023, 12:26 PM
Last Post: DeaD_EyE
  Formatting a date time string read from a csv file DosAtPython 5 5,049 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  How do I read and write a binary file in Python? blackears 6 25,276 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