WinError 5 Access is denied: 'C:\\Program Files (x86) - python

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)

Related

WinError 2: The system cannot find the file specified: (Cannot set explicit path to .exe file)

I am attempting to use the package WhiteboxTools. I have installed it to my python environment and imported it to my Jupyter notebook script. The whitebox documentation suggests explicitly setting the path to the executable file if it is not in the same folder as the script being written with the following:
wbt.set_whitebox_dir('/local/path/to/whitebox/binary/')
Another reason I believe this to be the problem is that I set the path to this file in my first code chunk: wbt.set_whitebox_dir('C:\\Users\\maxduso.stu\\Anaconda3\\envs\\geo\\pkgs\\whitebox_tools-2.2.0-py39hf21820d_2\\Library\\bin\\whitebox_tools.exe'), but the error arises in the next code chunk which calls one of the tools:
wbt.breach_depressions_least_cost(
"C:\\Users\\maxduso.stu\\Desktop\\FCOR_599\\project_work\\data\\tif_folder\\full_pa.tif",
"C:\\Users\\maxduso.stu\\Desktop\\FCOR_599\\project_work\\data\\tif_folder\\sa_breached_dem.tif",
dist = 10, #maximum search distancefor breach paths in cells
max_cost=None,
min_dist=True,
flat_increment=None,
fill=True
)
[WinError 267] The directory name is invalid: 'C:\\Users\\maxduso.stu\\Anaconda3\\envs\\geo\\pkgs\\whitebox_tools-2.2.0-py39hf21820d_2\\Library\\bin\\whitebox_tools.exe'
whitebox_tools is one of two files I attempted, the other being whitebox_gui. These files were suggested to me when I searched for ".exe" in the whitebox tools folder within my environment folder. That said, they are type = Application and so that brings me to the question: why did windows autofill "whitebox_tools.exe" in the file search bar but find an application file? Also, considering I cannot find a file of type .exe, where do I go from here?
Note: the tools also don't run without the path set.
Thanks for reading. I hope the question was clear enough.

How to fix "IOError: [Errno 2] No such file or directory" when making Virtual Environments

Whenever I try to use the 'virtualenv VirtualEnvironmentName' command or the 'virtualenv -p python3.8 VirtualEnvironmentName' command it says "IOError: [Errno 2] No such file or directory." I just want to make Virtual Environments, but I always get that error saying "No such file or directory."
Thanks in advance.
To create a virtual environment, you must specify a path.
Then you can activate the python environment by running the following command:
your_working_directory\\Scripts\\activate
Most likely, the problem is that you're using a relative path for the directory.
Let me clarify how Python finds files:
An absolute path is a path that starts with your computer's root
directory, for example 'C:\Python\scripts..' if you're on Windows.
A relative path is a path that does not start with your computer's root
directory, and is instead relative to something called the working
directory. You can view Python's current working directory by calling
os.getcwd().
Other common mistakes that could cause a "file or directory not found" error include:
You may be using escape sequences in a file path:
path = 'C:\Users\apps'
Incorrect! The '\n' in 'Users\apps' is a line break character!
To avoid making this mistake, you can use any one of the below methods:
use raw string literals
path = r'C:\Users\apps'
you can always use this:
'C:/Users/apps'
another possibility is:
'C:\\Users\\apps
if it keeps answering something like this
Try to uninstall and reinstall Anaconda, but now checking the box below
When creating the virtual environment, I was getting a error similar error.
I resolved it by removing anaconda from the PATH, and then adding the actual python dir.

JupyterLab - python open() function results in FileNotFoundError

I am trying to open an existing file in a subfolder of the current working directory. This is my command:
fyle = open('/SPAdes/default/{}'.format(file), 'r')
The filevariable contains the correct filename, the folder structure is correct (working on macOS), and the file exists.
This command, however, results if this error message:
FileNotFoundError: [Errno 2] No such file or directory: [filename]
Does it have anything to do with the way JupyterLab works? How am I supposed to specify the folder srtucture on Jupyter? I am able to create a new file in the current folder, but I am not able to create one in a subfolder of the current one (results in the same error message).
The folder structure is recognized on the same Jupyter notebook by bash commands, but I am somehow not able to access subfolders using python code.
Any idea as to what is wrong with the way I specified the folder structure?
Thanks a lot in advance.
There shouldn’t be a forward slash in front of SPAdes.
Paths starting with a slash exist high up in file hierarchy. You said this is a sub-directory of your current working directory.

Python: Script to delete folders from system32 or SysWOW64

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

How can i open a folder using python?

I've tried reading through the other answered similar questions, yet still can't find what I'm doing wrong.
file=open("Crawl","r")
cont=file.read()
file.close
print(cont)
It's as simple as that. Both the folder and python are on the desktop which explains why there isn't a directory to it. I did try opening .txt and .png files and that did work, but it seems I can't open folders?
PermissionError: [Errno 13] Permission denied: 'Crawl'
You are getting this error because you are trying to open "Crawl" as a file. It's a folder, and you can't treat a Windows folder as if it were a file.

Categories