Day 024 - Project 24 : Infinity Dice

If you have any questions, comments or issues with this project please post them here!

From: @QZapper :slightly_smiling_face:
I’m not sure where to begin. Recently I switched from Windows 10 to Windows 11.
On Windows 10, I completed Day 24 lesson 222, Absolute and Relative file paths and it runs just fine on Windows 10.
But when I run it on Windows 11 it fails.
I get, FileNotFoundError: [Errno 2] No such file or directory: ‘/Users/pilot/Desktop/my_file.txt’.
I’m using the same Code Syntax in both cases.
The path to the file on my Windows 11 is C:\Users\pilot\Desktop…
On Windows 10 the file path is C:\Users\El Lobo\Desktop and it runs with do problems.

I know its not a Version problem but a path issue. I just don’t know how to fix it.
How can I fix this?

with open("/Users/El Lobo/Desktop/my_file.txt") as file:
    contents = file.read()
    print(contents)

Correction, the correct file path is as follows:
'with open("../../../../Users/pilot/Desktop/my_file.txt") as file:
        contents = file.file.read()
        print(contents)'
Also I change the staring root directory and it now works.

with open("/Users/pilot/Desktop/my_file.txt") as file:
        contents = file.file.read()
        print(contents)

The same except for one is El Lobo the other has pilot.

Try adding C: to the start of your path, and switching the /s to \ but then using a raw string (so r"C:\Users\...")? Without being able to see your code it’s tricky to give a certain solution.

@QZapper

Using absolute path to open file.

Interesting that when I use C:/ in Win11 it finally worked. But on Win10 it works both ways.
Thank you.
:facepunch:

2 Likes

@QZapper

Using relative path to open file.

with open(“…/…/…/…/Desktop/my_file.txt”) as file:
contents = file.read()
print(contents)
FileNotFoundError: [Errno 2] No such file or directory: ‘…/…/…/…/Desktop/my_file.txt’

This is not working on my Win 11 but is on Win 10?

should it not be just ../../… (2 dots rather than 3)?

Using relative path to open file.
with open(“…/…/…/…/Desktop/my_file.txt”) as file:
contents = file.read()
print(contents)
I’m not sure how it originally copied three but that is not how I coded it. Again on Win10 it runs OK.
There is only 2dots not three. All total there is 8 dots. I tried adding C:/ on both Versions and in each one it failed. I guess it has something to do with Absolute and Relative paths.
Also tried taking out some of the …/ step by step each time running to see if I can catch the file but it didn’t work.
P.S. I just reviewed my reply and is seems for some reason its coping my snippet with 3 dots even though I’m sending it with two dots which is why you may see it as three. Sounds like a bug to me.

Try putting ` around your code snippets in here, perhaps discourse is converting .. into the tripple dot character.

With regards to your question, are you sure that the file actually exists, 4 folders up and then Desktop/my_file.txt.

Also, the file path will be relative to the folder from which you are running the program, and not guaranteed to to be the folder your program is actually in (say you’re in C:\Users\me and you run python myProgram/main.py, the paths will be relative to C:\Users\me rather than C:\Users\me\myProgram).

They see me rollin’ ( :game_die: ), they hatin’!!

https://replit.com/@JackAdem/Day-024-Project-24-Infinity-Dice?v=1

Day 24 of #Replit100DaysOfCode #100DaysOfCode.

1 Like

You are doing great @JackAdem ! Keep it up!

1 Like

Hi all, I’ve completed Day 24 of #Replit100DaysOfCode
https://replit.com/@claireweiz/day24100-days-Infinity-Dice-random-and-loop-function?v=1

I got stuck in the challenge section, but thanks to this community I still figured it out! So hopefully my coding/thinking process can do the same for others who are also on the learning journey.

And if anyone thinks my coding can be more clean, please let me know! Always aim for better coding skills :slight_smile:

1 Like