Access Denied error in python when trying to remove directory - windows

I'm trying to remove some directorys from my desktop.
The name of the directorys follow the format year+month (Ex.: 201808), i need to remove the directorys 2+ months before (Ex.: 201705, 201709, 201806).
Using the os.remove(path), the python return a error:
Traceback (most recent call last):
File "C:\Users\Usuario\Desktop\teste.py", line 36, in <module>
os.remove(caminhoPastaFinal)
PermissionError: [WinError 5] Acesso negado: 'C:\\Users\\Usuario\\Desktop\\Área de testes\\pasta1\\pasta2\\pasta3\\pasta4\\201712'
I tried to run the script as admin in the CMD, but i got the same error.
I'm using Windows 10.
Why i don't have permission to do that?
Follow the code:
import os
from datetime import *
def verificarNome(nomePasta):
mes=nomePasta[-2:]
ano=nomePasta[:-2]
if ano<anoAtual:
return True
elif mes<=mesAtual:
return True
return False
dataAtual = datetime.now()
anoAtual = str(dataAtual.year)
mesAtual = dataAtual.month
if mesAtual < 10:
mesAtual = "0"+str(mesAtual-2)
else:
mesAtual = str(mesAtual-2)
caminhoPai = 'C:\\Users\\Usuario\\Desktop\\Área de testes'
for caminhoPasta in os.listdir(caminhoPai): #Logo farei uma função recursiva que diminua esse código, mas ainda tenho que estudá-las
caminhoFilho1 = caminhoPai+"\\"+caminhoPasta
for caminhoPasta2 in os.listdir(caminhoFilho1):
caminhoFilho2 = caminhoFilho1+"\\"+caminhoPasta2
for caminhoPasta3 in os.listdir(caminhoFilho2):
caminhoFilho3 = caminhoFilho2+"\\"+caminhoPasta3
for caminhoPasta4 in os.listdir(caminhoFilho3):
caminhoFilho4 = caminhoFilho3+"\\"+caminhoPasta4
arrayPastasVerificar = os.listdir(caminhoFilho4)
for pastaFinal in arrayPastasVerificar:
if verificarNome(pastaFinal):
caminhoPastaFinal = caminhoFilho4+"\\"+pastaFinal
os.remove(caminhoPastaFinal)

import os
import shutil
import datetime
month = datetime.date.today().replace(day=1)
oneMonthBefore= (month - datetime.timedelta(days=1)).replace(day=1)
twoMonthBefore= (oneMonthBefore - datetime.timedelta(days=1)).replace(day=1)
root = 'C:\\Users\\Admin\\Desktop\\Directorys'
for path, dirs, files in os.walk(root):
for dir in dirs[:]:
try:
dirDate = datetime.datetime.strptime(dir, '%Y%m')
except ValueError:
continue
if dirDate < twoMonthBefore:
dirs.remove(dir)
shutil.rmtree(os.path.join(path, dir))

Related

ModuleNotFoundError: No module named 'pycaret.internal'

I am trying to deploy a ML model using Streamlit and Pycaret on Heroku.
When I try deploying the app, I get the following error:
ModuleNotFoundError: No module named 'pycaret.internal'
Traceback:
File "/app/.heroku/python/lib/python3.6/site-packages/streamlit/ScriptRunner.py", line 322, in _run_script
exec(code, module.__dict__)
File "/app/Final.py", line 11, in <module>
tuned_cat=joblib.load('Cat.pkl')
File "/app/.heroku/python/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 585, in load
obj = _unpickle(fobj, filename, mmap_mode)
File "/app/.heroku/python/lib/python3.6/site-packages/joblib/numpy_pickle.py", line 504, in _unpickle
obj = unpickler.load()
File "/app/.heroku/python/lib/python3.6/pickle.py", line 1050, in load
dispatch[key[0]](self)
File "/app/.heroku/python/lib/python3.6/pickle.py", line 1338, in load_global
klass = self.find_class(module, name)
File "/app/.heroku/python/lib/python3.6/pickle.py", line 1388, in find_class
__import__(module, level=0)
I have the following dependencies in the requirements.txt file:
pycaret==1.0.0
streamlit==0.58.0
I had to go with the pycaret version 1.0 because when I chose the latest version the slug size on Heroku would go beyond 500M and it would not deploy. Compiled slug size: 552M is too large (max is 500M).
Link to Github
Final.py:
import streamlit as st
import joblib
from pycaret.classification import *
tuned_cat=joblib.load('Cat.pkl')
import tensorflow as tf
physical_devices = tf.config.list_physical_devices('CPU')
def run():
add_selectbox = st.sidebar.selectbox(
"What would you like to do?",
("Online Prediction","Batch Prediction"))
st.sidebar.info('This app is created to predict if the applicant should be granted a loan or not.')
st.title("Loan Prediction App")
if add_selectbox == 'Online Prediction':
gender = st.selectbox('Gender',['Female','Male'])
married = st.selectbox('Married',['No','Yes'])
depend = st.selectbox('Dependents',['0','1','2','3+'])
edu = st.selectbox('Education',['Graduate','Not Graduate'])
self = st.selectbox('Self Employed',['No','Yes'])
app_inc = st.number_input ('Applicant Income')
co_inc = st.number_input ('Coapplicant Income')
amt = st.number_input ('Loan Amount')
term = st.number_input ('Loan Amount Term')
credit = st.selectbox('Credit History',['0','1'])
prop_are = st.selectbox('Property Area',['Rural','Semiurban','Urban'])
output=""
test_df = pd.DataFrame()
test_df['Gender']= [gender]
test_df['Married']=[married]
test_df['Dependents']=[depend]
test_df['Education']=[edu]
test_df['Self_Employed']=[self]
test_df['ApplicantIncome']=[app_inc]
test_df['CoapplicantIncome']=[co_inc]
test_df['LoanAmount']=[amt]
test_df['Loan_Amount_Term']=[term]
test_df['Credit_History']=[credit]
test_df['Property_Area']=[prop_are]
if st.button("Predict"):
Cat_pred=predict_model(tuned_cat,data=test_df)['Label']
output = Cat_pred.values
if(output==0):
text="Rejected"
st.error(text)
elif(output==1):
text="Approved"
st.success(text)
if add_selectbox == 'Batch Prediction':
file_upload = st.file_uploader("Upload excel file for predictions", type=["xlsx"])
if file_upload is not None:
data = pd.read_excel(file_upload)
st.success('File uploaded successfully!')
Cat_pred=predict_model(tuned_cat,data=data)['Label']
data['Prediction']=Cat_pred
st.write(data)
st.markdown(get_table_download_link(data), unsafe_allow_html=True)
import base64
def get_table_download_link(df):
"""Generates a link allowing the data in a given panda dataframe to be downloaded
in: dataframe
out: href string
"""
csv = df.to_csv(index=False)
b64 = base64.b64encode(
csv.encode()
).decode() # some strings <-> bytes conversions necessary here
return f'Download csv file'
if __name__ == '__main__':
run()

Why is pypandoc giving the error "RuntimeError: Missing format!" in this setup.py script?

I have a setup.py script for a module. This setup.py script features conversion of a Markdown README file to reStructuredText (suitable for PyPI). When an attempt is made to install this module using pip (sudo pip install supermodule), the following error is presented:
Collecting supermodule
Downloading supermodule-2017.1.12.2329.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-build-GijMOu/supermodule/setup.py", line 43, in <module>
main()
File "/tmp/pip-build-GijMOu/supermodule/setup.py", line 14, in main
long_description = pypandoc.convert("README.md", "rst"),
File "/usr/local/lib/python2.7/dist-packages/pypandoc/__init__.py", line 50, in convert
outputfile=outputfile, filters=filters)
File "/usr/local/lib/python2.7/dist-packages/pypandoc/__init__.py", line 68, in _convert
raise RuntimeError('Missing format!')
RuntimeError: Missing format!
What is going wrong and how can I fix this?
The setup.py script is as follows:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import pypandoc
import setuptools
def main():
setuptools.setup(
name = "supermodule",
version = "2017.01.12.2329",
description = "super module",
long_description = pypandoc.convert("README.md", "rst"),
url = "https://github.com/user/supermodule",
author = "A. Person",
author_email = "ap#sern.ch",
license = "GPLv3",
py_modules = [
"supermodule"
],
install_requires = [
"opencv",
"docopt",
"propyte",
"shijian",
"tonescale"
],
scripts = [
"supermodule_test.py"
],
entry_points = """
[console_scripts]
supermodule = supermodule:supermodule
"""
)
def read(*paths):
with open(os.path.join(*paths), "r") as filename:
return filename.read()
if __name__ == "__main__":
main()

Replace pickle with dill and pymongo call

I finally understood example how to replace pickle with dill from the following discussion: pickle-dill.
For example, the following code worked for me
import os
import dill
import multiprocessing
def run_dill_encoded(what):
fun, args = dill.loads(what)
return fun(*args)
def apply_async(pool, fun, args):
return pool.apply_async(run_dill_encoded, (dill.dumps((fun, args)),))
if __name__ == '__main__':
pool = multiprocessing.Pool(5)
results = [apply_async(pool, lambda x: x*x, args=(x,)) for x in range(1,7)]
output = [p.get() for p in results]
print(output)
I tried to apply the same philosophy to pymongo. The following code
import os
import dill
import multiprocessing
import pymongo
def run_dill_encoded(what):
fun, args = dill.loads(what)
return fun(*args)
def apply_async(pool, fun, args):
return pool.apply_async(run_dill_encoded, (dill.dumps((fun, args)),))
def write_to_db(value_to_insert):
client = pymongo.MongoClient('localhost', 27017)
db = client['somedb']
collection = db['somecollection']
result = collection.insert_one({"filed1": value_to_insert})
client.close()
if __name__ == '__main__':
pool = multiprocessing.Pool(5)
results = [apply_async(pool, write_to_db, args=(x,)) for x in ['one', 'two', 'three']]
output = [p.get() for p in results]
print(output)
produces error:
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Python34\lib\multiprocessing\pool.py", line 119, in worker
result = (True, func(*args, **kwds))
File "C:\...\temp2.py", line 10, in run_dill_encoded
return fun(*args)
File "C:\...\temp2.py", line 21, in write_to_db
client = pymongo.MongoClient('localhost', 27017)
NameError: name 'pymongo' is not defined
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/.../temp2.py", line 32, in <module>
output = [p.get() for p in results]
File "C:/.../temp2.py", line 32, in <listcomp>
output = [p.get() for p in results]
File "C:\Python34\lib\multiprocessing\pool.py", line 599, in get
raise self._value
NameError: name 'pymongo' is not defined
Process finished with exit code 1
What is wrong?
As I mentioned in the comments, you need to put an import pymongo inside the function write_to_db. This is because when the function is serialized, it does not take along any of the global references with it when it is shipped to the other process space.

(Python 2.7.6) IOError: [Errno 2] No such file or directory: OSX Yosemite

Sorry first time doing this -
Essentially I have obtained a code that can convert .wav audio files to an .stl file. I am trying to run "wavtotext.py" in Terminal, but end up with this IOError no file or directory error.
Can anyone please help?
Last login: Wed Mar 18 22:27:04 on ttys001
/var/folders/1_/q5syr5b51zn6y9yvxd7v8mhw0000gn/T/Cleanup\ At\ Startup/wavtotext-448370838.054.py.command ; exit;
Jags-MacBook-Pro:~ jag$ /var/folders/1_/q5syr5b51zn6y9yvxd7v8mhw0000gn/T/Cleanup\ At\ Startup/wavtotext-448370838.054.py.command ; exit;
Traceback (most recent call last):
File "/private/var/folders/1_/q5syr5b51zn6y9yvxd7v8mhw0000gn/T/Cleanup At Startup/wavtotext-448370838.051.py", line 24, in <module>
w = wave.open(fileName, 'r')
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wave.py", line 502, in open
return Wave_read(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/wave.py", line 159, in __init__
f = __builtin__.open(f, 'rb')
IOError: [Errno 2] No such file or directory: 'yourbody.wav'
logout
[Process completed]
This is my code:
import wave
import math
import struct
bitDepth = 8#target bitDepth
frate = 44100#target frame rate
fileName = "yourbody.wav"#file to be imported (change this)
#read file and get data
w = wave.open(fileName, 'r')
numframes = w.getnframes()
frame = w.readframes(numframes)#w.getnframes()
frameInt = map(ord, list(frame))#turn into array
#separate left and right channels and merge bytes
frameOneChannel = [0]*numframes#initialize list of one channel of wave
for i in range(numframes):
frameOneChannel[i] = frameInt[4*i+1]*2**8+frameInt[4*i]#separate channels and store one channel in new list
if frameOneChannel[i] > 2**15:
frameOneChannel[i] = (frameOneChannel[i]-2**16)
elif frameOneChannel[i] == 2**15:
frameOneChannel[i] = 0
else:
frameOneChannel[i] = frameOneChannel[i]
#convert to string
audioStr = ''
for i in range(numframes):
audioStr += str(frameOneChannel[i])
audioStr += ","#separate elements with comma
fileName = fileName[:-3]#remove .wav extension
text_file = open(fileName+"txt", "w")
text_file.write("%s"%audioStr)
text_file.close()
IOError: [Errno 2] No such file or directory: 'yourbody.wav'
The file 'yourbody.wav' is not found in the directory where you executed your program.

Error 1053 When Starting Window Service Written In Python

I have already looked at and tried the resolutions to this question that others have posted. One user said that to try and change my setup.py file from:
from distutils.core import setup
import py2exe
setup(console=["dev.py"])
to
from distutils.core import setup
import py2exe
setup(service=["dev.py"])
I got the following results:
running py2exe
*** searching for required modules ***
Traceback (most recent call last):
File "C:\Python27\Scripts\distutils-setup.py", line 5, in <module>
setup(service=["C:\Python27\Scripts\dev.py"])
File "C:\Python27\lib\distutils\core.py", line 152, in setup
dist.run_commands()
File "C:\Python27\lib\distutils\dist.py", line 953, in run_commands
self.run_command(cmd)
File "C:\Python27\lib\distutils\dist.py", line 972, in run_command
cmd_obj.run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 243, in run
self._run()
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 296, in _run
self.find_needed_modules(mf, required_files, required_modules)
File "C:\Python27\lib\site-packages\py2exe\build_exe.py", line 1274, in
find_needed_modules
mf.import_hook(mod)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 719, in import_hook
return Base.import_hook(self,name,caller,fromlist,level)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 136, in import_hook
q, tail = self.find_head_package(parent, name)
File "C:\Python27\lib\site-packages\py2exe\mf.py", line 204, in find_head_package
raise ImportError, "No module named " + qname
ImportError: No module named dev
Now, when I run py2exe with "console" in my setup script it works fine, but the service doesn't start and I get the error. When I run py2exe with "service" in my setup script py2exe doesn't run and tells me it can't find my module.
I have tried to re-install py2exe to no resolution. I have also tried to change:
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
to
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
Didn't make a difference either. CAN ANYONE HELP ME PLEASE? Here is what I am working on. It monitors a server and spits back a text file every 60 seconds which I use to monitor my servers at any given minute. Any help you guys and gals can give would be great.
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import wmi
import _winreg
from time import sleep
import os
class SrvMonSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "SrvMonSvc"
_svc_display_name_ = "Server Monitor"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
host = wmi.WMI(namespace="root/default").StdRegProv
try:
result, api = host.GetStringValue(
hDefKey = _winreg.HKEY_LOCAL_MACHINE,
sSubKeyName = "SOFTWARE\Server Monitor",
sValueName = "API")
if api == None:
raise Exception
else:
pass
except:
exit()
while 1 == 1:
with open("C:/test.txt", "wb") as b:
computer = wmi.WMI(computer="exsan100")
for disk in computer.Win32_LogicalDisk (DriveType=3):
name = disk.caption
size = round(float(disk.Size)/1073741824, 2)
free = round(float(disk.FreeSpace)/1073741824, 2)
used = round(float(size), 2) - round(float(free), 2)
for mem in computer.Win32_OperatingSystem():
a_mem = (int(mem.FreePhysicalMemory)/1024)
for me in computer.Win32_ComputerSystem():
t_mem = (int(me.TotalPhysicalMemory)/1048576)
u_mem = t_mem - a_mem
for cpu in computer.Win32_Processor():
load = cpu.LoadPercentage
print >>b, api
print >>b, name
print >>b, size
print >>b, used
print >>b, t_mem
print >>b, u_mem
print >>b, load
b.close()
date_list = []
stamp = time.strftime("%c",time.localtime(time.time()))
date_list.append(stamp)
name = re.sub(r"[^\w\s]", "",date_list[0])
os.rename("C:/test.txt", ("C:/%s.txt" % name))
try:
sleep(60.00)
except:
exit()
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(SrvMonSvc)
Have you progressed from your original problem. I had similar problem with a python service and found out that it was missing DLLs since the 'System Path' (not the user path) was not complete.
Running pythonservice.exe with -debug from the command prompt was not a problem because it used correct PATH environment variable, but if your service is installed as a System service it's worth checking out if the System Path variable has all the paths for the required DLLs (MSVC, Python, System32). For me it was missing the python DLLs path, after that it worked again.

Resources