py2exe os.chdir() - python

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.

Related

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

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.

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__)))

FileNotFoundError: [Errno 2] No such file or directory: 'test_text.txt'

The file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program. unless I am misunderstanding what the error means, I just can't figure out what i'm missing or did wrong. i'm just reading a basics python book and this I pretty much copied it 1 for 1.
CODE
with open('test_text.txt') as test_text:
reader = test_text.read()
print (reader)
FileNotFoundError means you are trying to open a file that does not exist in the specified directory (in this case, whatever directory you are running your Python script from).
the file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program
In that case, you need to make sure that you're in the same directory as your file on your command-line, or specify the full path to test_text.txt (e.g. /home/user/Desktop/test_text.txt)
FileNotFoundError occurs when you are trying to access files outside the scope of the application. In this case, the scope of your application is the folder in which you main python file resides.
There are couple of ways through which you can resolve this:
1. Provide an absolute or full path to the file: As mentioned by #pigeonburger in his answer, you can provide a full path. If you are using windows, then full path will be from C:/Desktop/
2. Using a Relative Path: Relative paths is the solution if both the file you want to access and the python program are in the same folder. Relative Paths start with ./ So, based on your file structure, the possible solution is this:
with open('./test_text.txt'):
# your code
Thank you Pointman for your answer. It did not work for me but it did lead me to what worked. I am using Visual Studio Code and I was having this issue with opening a text from a Udemy course.
jabber = open('Jabberwocky.txt', 'r')
for line in jabber:
print(line)
jabber.close()
After running this code I would get the following error message
FileNotFoundError: [Errno 2] No such file or directory: './Jabberwocky.txt'
I tried both the absolute path and the relative path option but they did not work for me. What I found that worked was once the terminal is open using Python you will have to traverse through your files until you get to the said folder or file. Once you do this then run the code and it will work.

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

Changing Directory With Python, Directory Not Found Error

I am trying to os.chdir() into system32 from python on windows, but when i attempt to change into this directory I am getting this error:
FileNotFoundError: [WinError 2] The system cannot find the file specified:
'/System32/
So obviously Python can't see this directory but I don't know why because os.listdir() shows this directory in the list. Does this have to do with the permissions that python has? Ultimately my goal is to change into the winevt directory to pull and dump the log files and to check for any errors, so any way to grab these is completely fine. My intuition was simply to change into the directory, open and read the log files and then check for errors, then print and report those errors.
Your current working directory may be different from the one where folder is.
Use this to check your current working directory before changing the directory.
print('Present DIR is : ',os.getcwd())
Then go to the correct directory and change the directory.
When you try to get into System32, use absolute path rather than the relative path, with the following:
os.chdir(r'C:/Windows/System32')
or in your case:
os.chdir(r'C:\Windows\System32\winevt\Logs')
As Archit said, you might not be in the correct directory.
The solution to this problem was a little bit hard to come by. I first tried uninstalling python 32 bit but that just broke everything.
I eventually installed python36 and added the python36.dll and the location of this dll to the user and system path (on Windows). Then I made sure to remove anything in the path involving python 34 or python36-32 which is the 32 bit version of python. This then allowed my to easily os.chdir into system32

Categories

Resources