Skip to content
Sign upLog in
← Back to Community

how do you reference images, sounds, folders, and other such files located in folders on a repl?

Profile icon
NY_ISLANDER_FAN

how do you reference images, sounds, folders, and other such files located in folders on a repl?

Answered by Nemoiscool [earned 5 cycles]
View Answer
Voters
Profile icon
gi0vanniM
Profile icon
JohnAdedeji147
Profile icon
ErinSterling
Profile icon
NY_ISLANDER_FAN
Comments
hotnewtop
Profile icon
Nemoiscool

You would do it the same way you would do it if the files were in your file system. But it depends on the programming language you are using, which you did not specify. For example:

If I was using JavaScript, Html, and Css, and I had an image, 'carrot.png,' inside a folder called 'images,' then I could reference it, assuming my file is in the same directory as the folder, by typing this: images/carrot.png.

however, if I was using python, and trying to open a file, I would do it differently. Say I have a text file, 'words.txt,' and it is inside a folder called 'textFiles,' to open the file in python I would do this: fin = open('textFiles/words.txt')

Hope that makes sense and answers your question!

Profile icon
NY_ISLANDER_FAN

@Nemoiscool
The language was Python; I'm so sorry I forgot to include that in my question, but even without it, you did an absolutely outstanding job answering and explaining in detail exactly how to do what I was asking!

That was a perfect explanation that completely answered my question! Thank you so much!! I have marked your response as the question's official answer and upvoted your response. Thanks again!!

Profile icon
NY_ISLANDER_FAN

@Nemoiscool
Although, I am using PyGame, so would it still be the same thing? I transferred over code from a project I made on the Python IDE Program on a Windows Computer originally so what would I change these type of things to? (Unless it would be the same thing)...

REPL.IT File Utilization Question -- Current Code

Profile icon
Nemoiscool

@NY_ISLANDER_FAN Ah, pygame is a bit different then python, normally in python you can't display images, so I decided to just explain how to use a text file.

I see that you are using pygame.image.load(), Which should be correct, and I see you're joining the variable img_folder and the image. Assuming img_folder is a string, and it is the correct spelling for the folder, there is only one thing I can see that might be wrong, although I can't test to find out for sure.

To explain, I'm going to assume the folder is called 'images', and that you've set img_folder = 'images' so that when you use it it goes into the correct folder to find the image. Doing path.join(img_folder,'powerupBlue_shield.png') then joins the strings together to create 'imagespowerupBlue_shield.png,' which is a file that I immediately assume doesn't exist.

My suggestion:
on the parts that say path.join(img_folder,'whateverImageName')
do this instead:
path.join(img_folder,'/whateverImageName')

This way, joining 'images' and /powerupBlue_shield.png returns 'images/powerupBlue_shield.png'

I am not sure if that is the problem, but I hope that helps!

Profile icon
NY_ISLANDER_FAN

@Nemoiscool
Thanks for responding so quickly!
So at the beginning, I have this:

REPL.IT File Utilization Question -- Current Code 2

Profile icon
Nemoiscool

@NY_ISLANDER_FAN I believe I was correct about the issue, every time you join a folder with anything, make sure the '/' character is in between them.

Profile icon
NY_ISLANDER_FAN

@NY_ISLANDER_FAN

@Nemoiscool
And these are the current File Folders I have, (everything's in them that I referenced, but I didn't want to expand the folders because then the list of files is literally like miles long and it would've been unnecessary.

REPL.IT File Utilization Question -- Current File Folders

Profile icon
Nemoiscool

@NY_ISLANDER_FAN Yeah, No problem, I don't need to see every file.

Profile icon
NY_ISLANDER_FAN

@Nemoiscool
So I did that, and ended up getting an error;

hup: redirecting stderr to stdout xset: unable to open display ":0" Keyboard Control: auto repeat: on key click percent: 0 LED mask: 00000000 XKB indicators: 00: Caps Lock: off 01: Num Lock: off 02: Scroll Lock: off 03: Shift Lock: off 04: Group 2: off 05: Mouse Keys: off auto repeat delay: 660 repeat rate: 25 auto repeating keys: 00feffffdffffbbf fadfffffffdfe5ef ffffffffffffffff ffffffffffffffff bell percent: 50 bell pitch: 400 bell duration: 100 Pointer Control: acceleration: 2/1 threshold: 4 Screen Saver: prefer blanking: yes allow exposures: yes timeout: 600 cycle: 600 Colors: default colormap: 0x20 BlackPixel: 0x0 WhitePixel: 0xffffff Font Path: /usr/share/fonts/X11/misc,built-ins DPMS (Energy Star): Display is not capable of DPMS nohup: ignoring input and appending output to 'nohup.out' nohup: appending output to 'nohup.out' pygame 1.9.6 Hello from the pygame community. https://www.pygame.org/contribute.html ALSA lib confmisc.c:767:(parse_card) cannot find card '0' ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default Traceback (most recent call last): File "main.py", line 17, in <module> player_img = pygame.image.load(path.join(img_folder, "/playerShip1_green.png")) pygame.error: Couldn't open /playerShip1_green.png

Although it only references the first one in the error, I presume the same thing would happen for all of them due to the fact that Python just runs in order so it caught that one first, threw the error, and then broke the program.

Profile icon
Nemoiscool

@NY_ISLANDER_FAN interesting... Try just typing in the file path instead of using the path.join().

Profile icon
NY_ISLANDER_FAN

@Nemoiscool
How do I only reference the file path in repl.it though —— instead of using that method...?

Profile icon
Nemoiscool

@NY_ISLANDER_FAN just type pygame.image.load('Images/whateverImage')