I've created an application (Python 3.6) with cx_Freeze and when ran as a Python command or even the executable works totally fine.
But if I make the application auto-boot on windows 10 (by creating shortcut in shell:startup) the log file it would create or write to raises PermissionError. I tried to create a workaround by checking if the file is locked, just add an increment number to the end of the file.
This also works, if I manually make a log file read only, it will create the next one and so on. But with auto boot it raises RecursionError, so it has probably absolutely no way to write to the disk.
Is there anyway without giving the application Run as administrator to be able to handle the magic of creating a log file when booting up? :)
Have you tried to let the application create the log file in the %APPDATA% directory or in the %LOCALAPPDATA%directory? You can use the following to get the path to these directories in Python:
import os
app_data_path = os.getenv('APPDATA')
local_app_data_path = os.getenv('LOCALAPPDATA')
print(app_data_path)
print(local_app_data_path)
See How can I get the path to the %APPDATA% directory in Python?
Related
A little bit of context:
I have made a password manager in python
I then packed it into a single executable using:
pyinstaller --onefile password_manager.py
Okay first I want to specify that the script works properly when I run it through the password_manager.py file, the executable version also runs properly and when I e.g. create a user and I save the data in a new json file. I'm able to access it on runtime but when I actually close the shell and the program stops running it's like the data has all been lost and or deleted. I'm looking for a way for the required data to remain on my drive like they do when I run password_manager.py on its own.
P.S.
I can view hidden files in my computer too.
P.S.S.
The link to the code is here: code
My file structure when I run it through password_manager.py
Here is the executable showing some random Data I recently added without closing and restarting the program. These information was supposed to be stored in /data/users.json and /data/password (and is stored there when I just use password_manager.py) but when I actually exit the application they are not present in any way when I rerun the executable(neither the directories that were supposed to be created nor the files).
Simple replication:
#mkdir.py
import os
os.mkdir("stack")
shell:
pyinstaller --onefile mkdir.py
I also sadly couldn't find a way to include the executable in this question but you can replicate it with the given command.
I currently have a Python script that scrapes some data from the internet, then saves it as a pickle file. When running this from a terminal with python filename.py it saves correctly, since the Date Modified field of the pickle file changes. However, when running with the built-in scheduler it doesn't actually save the pickle file, since the Date Modified isn't changed, despite seeing the Python script being executed (terminal opens up and I see the script running).
I ticked the Run with highest privileges box in the scheduler, and despite that it doesn't save the pickle file. I thought it had to do with it not having write permission, but if it has the highest priviliges, surely it can save a file?
At the scheduled time a terminal opens, so I know it is actually being executed (print a message to make sure), but it doesn't show an error about the fact that it couldn't save the file or anything like that. The only reason I know it's not working is the Date Modified field not changing. How can I fix this?
Windows Task Scheduler has a default working directory of C:\Windows\System32. If you set a relative path to the file you are trying to write, it will likely be written into that directory. If you open a Command Prompt in the directory of your script and run it, the relative path will be that directory. So, you actually have two copies of the pickle file.
If you set an absolute path in your script to the file you want to write to, both methods of running your script will write to the same file.
so my ultimate goal is to use python to read a specific application's windows event log when triggered by a file update.
Here is my problem, python I believe does not have access to the event logs stored in C:\Windows\System32\winevt\Logs. Whenever I try to read the files I get the following error:
WindowsError: [Error 2] The system cannot find the file specified
I tried every form of escaping, string split/join and using quotes on the file path and I always get the same error. I even cheaply used the os.system('dir "C:\Windows\System32..."') command in the python command prompt to list directories higher in the path for the log to verify access and I receive similar errors up to the C:\Windows\System32 directory, that one will list just fine.
Example:
C:\Windows\System32\winevt\Logs - File not found
C:\Windows\System32\winevt - File not found
C:\Windows\System32 - Lists files
I know these directories exist. So from here I figured I could use a bash script to copy the event log into a temp folder for python to read. I wrote a real simple bash script containing:
xcopy /Y "C:\Windows\System32\winevt\Logs\XXXXXXX" c:\Temp
(XXXXXXX) being the name of the log I want to copy for the python script.
The bash script runs fine on its own but when called by my python script it fails, refuses to copy the file because it can't find it. I have even tried using py2exe to create an exe to then run in administrator mode. This failed too. With similar errors of the file not found. I'm not sure I understand the permissions python uses to call the script and why the bash script cannot copy the file even when it can do it in a normal command prompt.
Any suggestions or flaws you can point out in my logic would be great. I am very stuck.
You are using a 32bits python on a 64bit install of windows.
In most cases, whenever a 32-bit application attempts to access %windir%\System32, the access is redirected to %windir%\SysWOW64
You can use os.listdir("c:\windows\sysnative\winevt\logs") as a workaround to read from the real system32 dir from a 32bit python interpretter runing on a 64bit windows...
Sources:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx
http://support.microsoft.com/kb/942589
I have some images that load in my python script.
they reside in c:\Python27\subfolder\images\
in my .py file (which resides in c:\Python27\subfolder\ )
I load them with ./images/file.jpg
It works just fine in IDLE
However when I run the file through notepad++ I get the error:
Failed to load file ./images/file.jpg
How can I fix this with out having to change my actual python code? (It works if I load the images with the full path, but I dont want to do this).
Im using the run command C:\Python27\python.exe "$(FULL_CURRENT_PATH) in notepad++
thank you very much!!
Well to help you fix the problem, you should do this
import os
print os.getcwd() #gets the current working directory
Most likely your problem is that within the IDE, your CWD differs than when you're running it from the console.
If you want it to work like it does in the IDE, you should first get yourself (the console) within that directory, via navigation (os.chdir(...) command).
You could also make a batch/bash file that runs the python script from the directory you need, and call that file from wherever you want (it would still call the python script with the path you gave
I made a simple python script which creates a text file. The contents of the script are
f = open("New", "w")
f.close
Now I moved the script to the desktop (I'm on a mac) and ran it, and nothing shows up. No file is created on the desktop that is visible for me.
I actually made this little script because one of my other scripts involves opening/creating a text file and reading from it. I entered the information wrong while in my program forever, and now the entire thing is broken and I cant fix it because I have no idea where it's created the darn text file. I was under the impression that just opening a file with giving it an absolute path would create it in the same directory as the script. I seem to have been mistaken.
I've been launching the script from the terminal with the command
python3 /Users/me/Desktop/script.py
Because of that, I feel like its creating the file somewhere in the python3 install location or within wherever the python3 unix exec is located. I think. Can't check.
Are any of you guys willing to help out?
-pipsqueaker117
EDIT: Here's a link to the big program which broke.
It'll be created in the current working directory, which is the directory from which you called the script.
Your file will be created in the current directory (most probably in /Users/me/ if you just opened the terminal)
Try:
cd /Users/me/Desktop/
python3 /Users/me/Desktop/script.py
You should modify your program to change the working directory (before you write the file) to the place where you want files to show up.
import os
os.chdir('/Users/me/Desktop') # or whatever
This should be a directory where you have permission to write files.
You can always ask:
import os
cwd=os.getcwd()
print(cwd)
That will print the directory the file (without a path) will be opened in.
You can change to a specific directory (in Python) this way:
import os
try:
os.chdir('/Users/me/Desktop')
except OSError as e:
print e