FileNotFoundError while using crontab to run Python script - python-3.x

I'm totally lost here. I'm trying to create a scheduler to run python script on my Mac, but I'm getting the following error:
Traceback (most recent call last):
File "/Users/Root/Desktop/Project/Data/script.py", line 148, in <module>
run(
File "/Users/Root/Desktop/Project/Data/script.py", line 121, in run
config = get_config("config")
File "/Users/Root/Desktop/Project/Data/config/__init__.py", line 3, in get_config
with open(f"config/{config_type}.json", "r") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'config/config.json'
So crontab convinces me that there is no such file or a directory, which is not true. I can run my script manually without errors.
My crontab is:
00 19 21 1-12 * /Library/Frameworks/Python.framework/Versions/3.9/bin/python3/ /Users/Root/Desktop/Project/Data/script.py >> /Users/Root/Desktop/Project/Data/cron.txt 2>&1
What am I doing wrong?
I'd be grateful for any help!
And is this possible without changing the relative path to an absolute path? I am aware of this solution

I assume crontab's cwd (Current Working Directory) is not same as where the script is stored.
this would solve your problem:
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(script_dir)
You can get the directory where you script is by calling "os.path.dirname(os.path.realpath(file))"
if you change the current working directory "os.chdir(...dir...)" you can access you config/config.json by relative path,
Otherwise you will have to use a absolute path
Try running this and check your output file:
import os
script_dir = os.path.dirname(os.path.realpath(__file__))
print (os.getcwd())
print(script_dir)
os.chdir(script_dir)
print (os.getcwd())

Try to open with full path to the json file.

Related

FileNotFoundError in unzipping python file

I need to automate some boring stuff , one of such is unzipping all zip file in the current directory
this is my code:
import os
import zipfile
directory = 'D:\\Python ds and alg by mostafa'
for file in os.listdir(directory):
if file.endswith('.zip'):
zipfile.ZipFile(file).extractall(directory)
however when i run this code I have this error:
Traceback (most recent call last):
File "D:/Python Automation Files/extract_zip_files.py", line 7, in <module>
zipfile.ZipFile(file).extractall(directory)
File "C:\Python310\lib\zipfile.py", line 1247, in __init__
self.fp = io.open(file, filemode)
FileNotFoundError: [Errno 2] No such file or directory: '08_Logical_and_physical_Data_Structures.zip'
The problem seems to be that you try to extract the file '08_Logical_and_physical_Data_Structures.zip' which is not located in the same folder as your script (because its in your directory you defined). So you find it while you search for it (because here you search in the correct directory) but in the line where you try to extract it you dont tell python to extract the file which is located in the directory. So it should work if you change your code to:
import os
import zipfile
directory = 'D:\\Python ds and alg by mostafa'
for file in os.listdir(directory):
if file.endswith('.zip'):
zipfile.ZipFile(directory + file).extractall(directory)
or to be safe you could use os.path.join(directory, file)
Edit: because I just saw it. You try to extract the file:
D:/Python Automation Files/08_Logical_and_physical_Data_Structures.zip
but your code should extract:
D:\\Python ds and alg by mostafa\\08_Logical_and_physical_Data_Structures.zip

Python: FileNotFoundError: [Errno 2] No such file or directory - How to add file to the right directory

I'm new to python! Seen many issues related to this problem but can't find the right way of doing it.
I want to import a picture and change it.
My code is:
from PIL import Image, ImageFilter
import os
root_dir= os.path.dirname(os.path.abspath(r'C:\Users\User\eclipse-workspace\Practice Python CS50 2019\images\Mario.png'))
before = Image.open('Mario.png')
after=before.filter(ImageFilter.BLUR)
after.save("MarioBLUR.png")
The error I'm getting is:
Traceback (most recent call last):
File "C:\Users\User\eclipse-workspace\Practice Python CS50 2019\src\Class 6\blur.py", line 5, in
before = Image.open('Mario.png')
File "C:\Users\User\anaconda3\lib\site-packages\PIL\Image.py", line 2809, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'Mario.png'
My windows location for this picture is: C:\Users\User\Downloads\Mario.png
My eclipse location is: C:\Users\User\eclipse-workspace\Practice Python\images\Mario.png
How to add this picture to the right directory to make sure I won't have this issue anymore?
You only need the directory path and not the filename in os.path.dirname, for example:
root_dir= os.path.dirname('C:/Users/User/eclipse-workspace/Practice Python CS50 2019/images/')
before = Image.open(root_dir + 'Mario.png')
should work fine

FileNotFoundError: [WinError 2] The system cannot find the file specified while loading model from s3

I have recently saved a model into s3 using joblib
model_doc is the model object
import subprocess
import joblib
save_d2v_to_s3_current_doc2vec_model(model_doc,"doc2vec_model")
def save_d2v_to_s3_current_doc2vec_model(model,fname):
model_name = fname
joblib.dump(model,model_name)
s3_base_path = 's3://sd-flikku/datalake/current_doc2vec_model'
path = s3_base_path+'/'+model_name
command = "aws s3 cp {} {}".format(model_name,path).split()
print('saving...'+model_name)
subprocess.call(command)
It was successful, but after that when i try to load the model back from s3 it gives me an error
model = load_d2v("doc2vec_model")
def load_d2v(fname):
model_name = fname
s3_base_path='s3://sd-flikku/datalake/current_doc2vec_model'
path = s3_base_path+'/'+model_name
command = "aws s3 cp {} {}".format(path,model_name).split()
print('loading...'+model_name)
subprocess.call(command)
model=joblib.load(model_name)
return model
This is the error i get:
loading...doc2vec_model
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in load_d2v
File "C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 339, in call
with Popen(*popenargs, **kwargs) as p:
File "C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 800, in __init__
restore_signals, start_new_session)
File "C:\Users\prane\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1207, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
I don't even understand why it is saying File not found, this was the path i used to save the model but now i'm unable to get the model back from s3. Please help me!!
I suggest that rather than your generic print() lines, showing your intent, you should print the actual command you've composed, to verify that it makes sense upon observation.
If it does, then also try that exact same aws ... command directly, at the command prompt where you had been launching your python code, to make sure it runs that way. If it doesn't, you may get a more clear error.
Note that the error you're getting doesn't particularly look like it's coming from the aws command, of from the S3 service - which might talk about 'paths' or 'objects'. Rather, it's from the Python subprocess system & Popen' call. I think those are via your call tosubprocess.call(), but for some reason your line-of-code isn't shown. (How are you running the block of code with theload_d2v()`?)
That suggests the file that's no found might be the aws command itself. Are you sure it's installed & runnable from the exact working-directory/environment that your Python is running in, and invoking via subprocess.call()?
(BTW, if my previous answer got you over your sklearn.externals.joblib problem, it'd be good for you to mark the answer as accepted, to save other potential answerers from thinking that's still an unsolved question that's blocking you.)
try to add extension of your model file to your fname if you are confident the model file is there.
e.g. doc2vec_model.h3

PermissionError: [Errno 13] Permission denied while running a server-side CGI scripts coded in Python

I am trying to run a server-side cgi script coded in python but I am getting the following error while running it.
Traceback (most recent call last):
File "webserver.py", line 16, in <module>
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 449, in __init__
self.server_bind()
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/server.py", line 137, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socketserver.py", line 463, in server_bind
self.socket.bind(self.server_address)
PermissionError: [Errno 13] Permission denied
Here is the script that I am trying to run.
"""
Implement an HTTP web server in Python that knows how to run server-side
CGI scripts coded in Python; serves files and scripts from current working
dir; Python scripts must be stored in webdir\cgi-bin or webdir\htbin;
"""
import os,sys
from http.server import HTTPServer, CGIHTTPRequestHandler
webdir = '.'
port = 80 #default http://localhost/, else use http://localhost:xxxx/
os.chdir(webdir)
srvraddr = ("",port)
srvrobj = HTTPServer(srvraddr,CGIHTTPRequestHandler)
srvrobj.serve_forever()
Any help will be appreciated. Thanks in advance.
Use this
chmod 777 script_name,
then execute the script if it fails again then use
sudo python script_name
or check how to run script using administrator privileges on your respective operating system.

Python: Cannot Create Zip File

I simply copy-and-pasted this code from a Python tutorial website, but the code won't work. What's missing? I am using version 3.4.3. Thank you.
import zipfile
# Create zip file
print("Creating zip archive")
zf = zipfile.ZipFile("python_zip_file.zip", mode = "w")
try:
# Add file to our zip
zf.write("zippy2.py")
finally:
print("closing")
zf.close()
Traceback (most recent call last):
File "/Users/Cindy/Documents/Python/Zip.py", line 9, in <module>
zf.write("zippy2.py")
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/zipfile.py", line 1326, in write
st = os.stat(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'zippy2.py'
# Add file to our zip
zf.write("zippy2.py")
You should have a file named zippy2.py in the folder.
Since you just copied the code, you might not have the file that was mentioned in the code. create file
zippy2.py in the same folder and check.
Try learning with this..
#!/usr/bin/env python
import zipfile
print("Creating zip archive")
zip = zipfile.ZipFile(‘Archive.zip’, ‘w’) #Archive is the name of the zip file
zip.write(‘file.txt’) #file.txt should be in the current working directory
zip.write(‘file1.txt’) #file1.txt too
zip.close()

Resources