#๐ Python relative path in Onedrive/Sharepoint
28 messages ยท Page 1 of 1 (latest)
@chilly owl
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
it's probably a problem in your code
It works when the folder is on my desktop
so you found a solution
No i did not. It does not work when on Onedrive. Thats my problem
I see. I know it's not what you're asking for, but may I suggest not putting your code on onedrive?
if you want to share your code with other people there are better ways.
It's the company storage and backup, so "better" ways doesn't really help me
I'm just wondering if anyone has had similar issues. I'm assuming Microsoft maybe does some funky syncing tricks that i dont know of
The paths are relative to the project files. No directory paths
I don't think that's the case, still it's probably a problem with your code
try using __file__ for the working directory
Right
Does that exist?
It does, because the program works when its located on my desktop, or anywhere else on my C drive
Like this?
os.listdir('__file__/firmware_library')
no. give me a second
I'm pretty sure relative paths are relative to the cwd, not the location the script is in
Is there a macro that tells it relative to this file?
it should be relative to the place of the script
They are
__file__ is a variable that should be the abspath to the script
you can combine that with pathlib for something like
from pathlib import Path
script_dir = Path(__file__).parent
import os
from pathlib import Path
directory = Path(__file__).absolute().parent
print(os.listdir(directory))
I will try ๐
or
from pathlib import Path
directory = Path(__file__).parent
print(list(directory.iterdir()))
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.