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.
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 was working on a python script that I want to run via the task manager. I created a batch file like this:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe" "\\insert_path_here\project_directory.py"
pause
When I have the project_directory.py as a path on my hard drive, it works fine with either the task manager or manually opening the batch file. When I put the .py file on a remote drive, the batch file still executes correctly when I manually open it. When the task manager tries runs the .bat file, I get a message asking which application I want to use to run the program:
What do?
Thanks!
Try using cmd.exe. on default you should find it here: c:\windows\system32\cmd.exe.
On the side note explore option of converting .py to .exe, you can start here: https://www.simplifiedpython.net/convert-python-to-exe-tutorial/
I feel like it would be better option for you.
It looks like it is trying to run the python.exe and then the project_directory.py file separately. Try putting those in as one command:
call "C:\ProgramData\Anaconda3\Scripts\activate.bat"
"C:\ProgramData\Anaconda3\python.exe \insert_path_here\project_directory.py"
You probably have *.py files already associated with Python on your local machine which is why it works there.
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?
I have tried writing some content to a file by running a python code in a batch file.I have used the following as the batch command.
python C:\Python27\filtercsv.py
When I tried running the program in IDLE or in the command prompt, it was writing to the file. But, when I tried the same using the batch file, it wasn't writing.
I have included python in the PATH environment variable. So, I guess I don't need to specify the .exe file's location.
Can anyone tell me how to solve this issue?
Do you write the file to an absolute path?
If not you might want to either change the current directory in the batch file before running the script:
pushd C:\Python27
python filtercsv.py
popd
or include a path in the file name you're writing to.
This is because of if you write to a file just by name it will show up in the current working directory, which is not necessarily where the script is located.
(However, you should probably not save your scripts in your Python installation directory, nor write files there because you don't usually have write access there.)
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