Python correctly finding and reading windows application event logs - python

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

Related

Running Python on Ubuntu Shell

I am trying to run python on Ubuntu. ADDED: It's a double boot system with Windows.
If I type python on the shell, it opens python. But I want to run a python file.
I have my python (.py) file saved on windows Desktop.
On Windows when I run a .py file in my command prompt, I just have to change directory to Desktop then type python myfile.py and the code runs successfully.
When I try to do that same thing in Ubuntu, it does not work. I made a Desktop directory in Ubuntu using the mkdir function.
Now, when I type python myfile.py, I get the error:
python: can't open file 'myfile.py': [Errno 2] No such file or
directory
I tried typing python on the Ubuntu shell then dragging the myfile.py file (~$ python C:\Users\username\Desktop\myfile.py), it used to run and then close the answer right away, but now I would get the error
python: can't open file 'C:UsersusernameDesktopmyfile.py': [Errno 2]
No such file or directory
Can anyone tell me what are the exact steps I need to do to fix this?
EDIT:
Here is what I am writing from the answers below:
~$ python /home/username/Desktop/myfile.py
Yet, I am getting this error:
python: can't open file '/home/username/Desktop/myfile.py': [Errno 2] No such file or directory
EDIT 2**
So here is something new:
if I write
python /home/username/Desktop myfile.py
I get this error
/usr/bin/python: can't find 'main' module in '/home/username/Desktop
If I write
python /home/username/myfile.py
I do not get an error, but I do not get any output either. Ubuntu just goes to the next line $
Strange on Ubuntu your path starts with c:\...
On Ubuntu user folders are usually in /home, user folder can be referenced by ~, so IMHO python ~/Desktop/myfile.py should work in your environment.
EDIT: noted you made Desktop folder, not original Desktop, that way when you are in that folder type pwd it will show full path, then put it in python PATH/myfile.py (and just in case you may type ls to show list of files in current folder on Linux to check you are indeed in the right folder where your program is).
ADDED: after discussion it turned to be double boot system, mount showed mounted windows disk and file.py was found and run!
Backslash '\' is an escape character, in Unix it will not be used if you put it in a path. It results in your error of path not found.
Use slash '/', your code should run.
It is weird that you have a path for windows, in Unix you should not have this type of path...
Open a terminal, go to the folder with your python script. Use pwd in the terminal to know the exact location and then copy the path and use the following (I take an example here):
python PATHTOYOURPYTHONSCRIPT/mypythonscript.py
You're trying to use windows path representation in Linux. Windows and Linux has different path representations.
In windows, you can use C:\ but in Linux it's just a / which is used to denote the root directory.
In the terminal, type 'pwd' where your python file is present in Ubuntu and you'll see the output as '/home/username/Desktop' which is not like windows.
So you need to run like, 'python /home/username/Desktop/my file.py'.
If you need to access the file present in the windows partition, you need to mount the windows partition. This can be done using the Files app present in Ubuntu. After that, you can navigate to /mnt/media/ and find your file.

cx_Freeze created executable has no write permission on boot

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?

Running Shell Script from Applescript

I want to run a Shell script using Automator in OSX. The shell script calls a Python file which is located in the folder "/Users/Tex/Python". My code is:
on run {input, parameters}
tell application "Terminal"
do shell script "/Users/Tex/anaconda3/bin/python /users/Tex/Python/MoveFiles.py $#"
end tell
return input
When I execute the automator script I get an error as follows:
Terminal got an error: /users/Tex/anaconda3/bin/python:
can't open file '/users/Tex//Python/Move_Files.py': [Errno 2] No such file or directory
I suspect that the problem is that the "/Users/Tex" folder is located on a different drive to the OSX operating system but that the Shell interpreter is not aware of this. I have tried using the full path to the python file, i.e. including the volume name (iMac Disk A) however the fact that this has spaces in it seems to create yet another issue for the Shell.
Appreciate feedback on how to make thsi work!

Python File Directory Can't be found

When I try to run my python file, I get the following error: "can't open file 'hello.py': [Errno 2] No such file or directory" I have tried cd and it shows that my file is in the Users/ierdna/ directory. I have the python program on my desktop and I still cannot run it.
Thanks very much!
It seems that I have tried everything, and nothing is working. :(
I am going to assume you know some of the basic BASH command line commands. If you don't check them out here.
Opening your terminal's respective shell, enter the following on your command line:
cd Desktop [to change directory to your desktop]
ls [to list all the directories and files on your desktop, to make sure your hello.py file is in fact there]
python hello.py [to run your python file]
That should run it. Let me know if you run into errors.
In order to properly answer this question the following information are required:
a. OS that you are using
b. Release version of that OS
c. Python version that you are using
d. If your machine has at least 10GB of free space
Kidding!
You just need to use cd ~/Desktop to make the 'Desktop' your working directory and then try to run python hello.py Alternatively you can also try running python ~/Desktop/hello.py directly without using 'cd' command. Note: In order to run a python script you need to provide the path(Either complete path, for example: python /home/username/Desktop/script.py or relative path, for example: python ../script.py) to the script. If you just provide the script name, it will fail unless the script exists in the current working directory. Also, kindly do check for existing questions and answers before posting your own question as I doubt this question is new and hasn't been answered correctly before.
I'm going to assume from you saying you used cd that you are using Mac or Linux. This solution will work for both. If I am wrong and you are running Windows, just comment it and I'll change the answer. On to the real answer:
First open your terminal, then type cd ~/Desktop. Now try running your python script.
EDIT:
Apparently you are running Windows. OK. I'm going to leave the above answer for other people who have the same problem on Mac or Linux. What you need to do is execute this command in your command prompt cd C:\Users\[your user name]\Desktop. Replace [your user name] with your actual user name. Then run your python script (python hello.py)

failed to set __main__.__loader__ in Python

When running any Python script (by double clicking a .py file on Windows 7) I'm getting a Python: failed to set __main__.__loader__ error message. What to do?
More details:
The scripts work on other machines.
The only version of Python installed on the machine on which the scripts don't work is 3.2.
I get the same error when trying to run the script from the Windows shell (cmd).
Here's an example for the content of a file named "hey.py" that I failed to run on my machine:
print('hey')
I had the same problem. Turns out it was because the file was stored in a directory with a name not in English (Hebrew in my case).
Make sure the path to the file contains only English letters.
http://bugs.python.org/issue16218
I also had this problem. Like mottyg1 said, it happens when the python script is run from a directory containing non-english characters. I can't change the directory name though, and my python script needed to be in the directory in order to perform manipulations on the filenames. So my workaround was simply to move the script to a different folder and then pass in the directory containing the files to be changed.
So to be clear, the problem is only when the directory containing the python file has non-english characters, but python can still handle such characters in its functions, at least as far as I've been able to tell.

Categories