Why this Relative Path of a file is not working (..\) and (.\) - python

I have been working on a project but while opening files from different folders I came across this peculiar problem where the relative path for a file within the same directory is not opening with this statement:-
But works perfectly fime with this statement:-
even when I don't mention folder description, like directly using the file name in statement, it shows error:-
Can anyone give a explaination for what is going wrong here?
Also this file is working in this particular location only, i.e. if I move the complete project to another location on the computer or to another computer, all the methods fail and I need to mention absolute path in order to make it work.

Related

Is there a way to load images without using the root file in pygame?

When I load images using pygame, I normally just use file locations such as: 'B:\Python\Images\image.png' but if I want this to be able to run on different computers without needing to edit the code every time, I would just want to go like to do 'image.png' instead. Is there a way to do this? Whenever I try to skip the B:\Python\' part, I get the error: "File Error: No file "image.png" found in working directory 'C:\WINDOWS\System32."
I've seen people use a file location such as: '../Images/image.png' instead but that didn't work (I may have done it wrong).
You have to set the working directory. The resource (image, font, sound, etc.) file path has to be relative to the current working directory. The working directory is possibly different to the directory of the python script. The name and path of the python file can be retrieved with __file__. The current working directory can be set with os.chdir(path).
Put the following at the beginning of your main script to set the working directory to the same as the script's directory. This allows you to specify file paths relative to your main file:
import os
os.chdir(os.path.dirname(os.path.abspath(__file__)))

Is there a way to specify the actual location of a file compiled with Pyinstaller (--onefile) and not the temporary _Meixxxx folder

Recently I've been working on a Python project that requires human interaction with other files. I compiled it to one file. I tried to get the absolute path to that compiled file. However, everything the internet has suggested leads me to the _Meixxxx temp folder. Although useful, it's not very well situated for human interaction. Is there any way that I can find the absolute path to my exe file and not the _Meixxxx folder?
I've tried several things including:
os.cwdir
os.path.dirname
Thank you for any help in advance
EDIT:
Thanks to #johnashu
os.path.dirname(os.path.realpath(__file__))
This seems to correctly find the path of the file location
As #johnashu suggested
os.path.dirname(os.path.realpath(__file__))
should give you the actual location for your exe file

Why I need to specify working directories and path?

Whenever I do a project for computer science, I have to make sure all of my files are located in the same folder, or I'll have errors. If I want to use a file from somewhere else, I have to insert it into the path. I do these things but don't fully understand what is happening or why. Why is the path changed in the runtime environment?
When you run a python script you are executing it in the current working directory /home/user/python.py for example. That means this script since it lives in /home/user has access to everything in that path. However you should be able to access any other directory from here as long as the permissions are setup right. You would do that by using relative paths. so for example /home/user/python.py could access a file that is /home/example/file.txt by giving it the path ../example/file.txt from the python project.
Have you tried adding the path using sys.path.append? If you don't want to do that every time then you can set (Windows) %PYTHONPATH% to include your custom path. That's what I do for my include folder.

Where does "/folder1/folder2/file.ext" save to in Python?

I know there must be a really obvious answer, but it eludes me. Where does "/folder1/folder2/file.ext" save to in Python? In some code I thought (incorrectly) that it would save the file to a relative path, but when I checked folder2 it wasn't there. However, it seemed to still take up the hard disk space. I did a search on my hard drive and couldn't find the file. Where did it go? Again, I'm aware that there must be a really obvious answer out there, but I am just learning to code.
Firstly I would read about the os module and the module search path.
I opened the IDLE 3.7.0 on my Windows 10 machine and did the following:
>>> import os
>>> os.getcwd()
'C:\\Python37'
>>> os.path.abspath('./folder1')
'C:\\Python37\\folder1'
Windows expands the . to represent the current working drive, thus wherever the interpreter is looking is where your folders will reside.
In the links, it talks about how sys.path works (in short the directory containing the current script is placed at the front of it [e.g. the current working directory] for searching).
So, in short, either run your script directly from the directory or call os.chdir('/your/desired/path').

py2exe os.chdir()

When I was building my program in Python, every file that the program needs was in the root folder, but I decided to re-organise the files in folders, so I create a folder "images" to storage here the favicon and the other images that the program use.
I use os.chdir() to move through the images folder and define the image element, like this.
#other code
actualdir = os.getcwd()
imgdir = actualdir + "\images\\"
os.chdir(imgdir)
mywindow.iconbitmap('favicon.ico')
os.chdir(actualdir)
#othercode
Ok, so, if I run the program with the Python interpreter, there aren't any problems with this, but, for this program I need to compile an exe version.
I use py2exe to solve this issue, and when I run the exe I got this error on the log file.
FileNotFoundError: [WinError 2] The system cannot find the file specified:
'C:\\MyProgram\\Version1.0\\images\\'
And it's logical that it can't find the file cause the path is incorrectly defined, using two bars instead of one, but, why my program gives this response, using two bars. The function gives the path and it uses, why is this happened?
Hope something could help me.

Categories

Resources