Creating folders adds them to the wrong directory

Lots of problems today

I have a couple of functions in Python for creating directories, but they are being created in Replit’s upmost directory.

src -
    createFile.py # Creates a folder
createdFolder -
    contents -
        file

Shouldn’t the created folder be within src? I’ll link my Repl below if it is necessary to troubleshoot (in boot.py).

https://replit.com/@bobbypac/pyos3

2 Likes

It depends, if you’re in the src dir the folder should be created there. If you’re not in the src dir the folder will not be created there.

3 Likes

maybe try using ./ so it creates it in the current directory

3 Likes

No luck on this one. Every function call that seems to try to access a file fails because it seems to prefer root over its current directory.

2 Likes

Try running print(os.getcwd()), and see what that returns.

3 Likes

Yep, it outputs the project’s root directory, the reason it creates directories there. Yes, I could just add src/ to every function call, but that is a pretty ugly solution IMO.

3 Likes

Idea:

cwd = os.getcwd()+"src/"

# I don't know what this function is.
mkdir(cwd+"Some folder")
2 Likes

Yes, this would work, but I have to think about it in terms of the user, so I actually wouldn’t be able to do that, considering an actual user would only copy the contents of src/, I wouldn’t be able to do that (At least I think).

Now, it doesn’t really matter where that store folder gets made, but it is a huge issue when using file_exec() because the other source code is stored within src/. I try to keep my code unreliant on the repository itself, I only use version control as a way to store my code, not to give it an environment.

1 Like

Type in the shell:

cd yourparentfolder && mkdir yourfolder
1 Like

Yes, I could do that but again, the user may not copy over src/ in the first place. If the user were to only download the contents of src/, my code would fail because src/ wouldn’t exist to write into.

1 Like

What is this supposed to mean?

1 Like

My code is structured in that of a Git repository. It seems to be making directories into the projects root directory when it should be making it inside of the Python script that called the function to make the directory in the first place, I can’t figure out why this is happening though, and I don’t want to add src/ to every function call for the reasons I explained above.

1 Like

Ok, I don’t understand anymore. :question:

1 Like

Sorry, I worded it pretty unclearly. Let me give you an example:

src -
    createFile.py # Calls a function to create a folder
    # createdFolder should have been created in this directory
createdFolder - # But it was created here for some reason
    contents -
        file
1 Like

Like I said (I’m having trouble with understanding the last 2 posts)

1 Like

Ok, let’s say a user came to download my project off of my Git repository. They probably won’t need to copy the whole repository over, and would just take the code itself, or download the release. The problem is, if I do every function call like so: make_dir("src/newFolder/"), src/ won’t exist to write into.

2 Likes

what does this mean???

:question: :question: :question:

1 Like

src/ is the name of the folder I am storing the source code in on my Replit (which is causing the problem) and Git repository. If I am correct, a Python script would create a folder within its current directory if you did something like this on your local machine, but it’s writing to the project’s root in this case.

2 Likes

It’s been a bit, but I saw @Firepup650’s post and it seems to be outputting /home/runner/pyos3 instead of the expected /home/runner/pyos3/src. I have no idea why this is happening, and I found nothing after googling.

This would happen locally, so why doesn’t this work on Replit? I’m really confused

As long as you’re fine with directories being created based on where the script is, this should work:

import pathlib

path = pathlib.Path(__file__).parent.resolve()

Then, just reference the path variable when you need to create a new directory.

From Stack Overflow

1 Like