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
Related
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.
I had a python script main.py it did something and to run it via crontab on a daily basis I created the following file (I think it's called bash script):
#!/bin/sh
source /Users/PathToProject/venv/bin/activate
python /Users/PathToProject/main.py
For some time now it ran daily without any problems.
Now I added a feature that saves a .CSV file containing some results to my google drive via PyDrive2 afterward in the main.py. When running this new script via command line it runs successfully without any errors - every time.
I assumed that the crontab would run as well, but now I get the Traceback below.
/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/_helpers.py:255: UserWarning: Cannot access mycreds.json: No such file or directory
warnings.warn(_MISSING_FILE_MESSAGE.format(filename))
Traceback (most recent call last):
File "/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/clientsecrets.py", line 121, in _loadfile
with open(filename, 'r') as fp:
FileNotFoundError: [Errno 2] No such file or directory: 'client_secrets.json'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 431, in LoadClientConfigFile
client_type, client_info = clientsecrets.loadfile(
File "/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/clientsecrets.py", line 165, in loadfile
return _loadfile(filename)
File "/Users/PathToProject/venv/lib/python3.8/site-packages/oauth2client/clientsecrets.py", line 124, in _loadfile
raise InvalidClientSecretsError('Error opening file', exc.filename,
oauth2client.clientsecrets.InvalidClientSecretsError: ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/PathToProject/main.py", line 5, in <module>
main()
File "/Users/PathToProject/version2.py", line 20, in main
PYD.download_file(data_file)
File "/Users/PathToProject/PyDrive_Modul.py", line 58, in download_file
file_ID = get_ID_of_title(filename)
File "/Users/PathToProject/PyDrive_Modul.py", line 47, in get_ID_of_title
drive = google_drive_auth()
File "/Users/PathToProject/PyDrive_Modul.py", line 11, in google_drive_auth
gauth.LocalWebserverAuth()
File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 123, in _decorated
self.GetFlow()
File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 507, in GetFlow
self.LoadClientConfig()
File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 411, in LoadClientConfig
self.LoadClientConfigFile()
File "/Users/PathToProject/venv/lib/python3.8/site-packages/pydrive2/auth.py", line 435, in LoadClientConfigFile
raise InvalidConfigError("Invalid client secrets file %s" % error)
pydrive2.settings.InvalidConfigError: Invalid client secrets file ('Error opening file', 'client_secrets.json', 'No such file or directory', 2)
If I edit the python script and skip the part of up/downloading to google drive it works fine.
Now I don't know why this error occurs and how I can solve this problem. The error message seems to be misleading because the client_secrets.json is in the directory and it works via the command line.
When you run via command line it picks path for json file and others.Cron could not find path.Be absolute in path, It will run smoothly. If absolute path not possible, try relative path with respect to CRON location path.
So I'm trying to create a web map, and every time I get to the point of trying to save the map object as a HTML file, I get an error telling me "Permission denied." What could I be doing wrong here?
import folium
>>> map = folium.Map(location=[80, -100])
>>> map
<folium.folium.Map object at 0x00B8AF70>
>>> map.save("Map1.html")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\austi\AppData\Local\Programs\Python\Python38-32\lib\site-packages\bran
ca\element.py", line 161, in save
fid = open(outfile, 'wb')
PermissionError: [Errno 13] Permission denied: 'Map1.html'
I think that it is due to the fact that you have not created your HTML file at the right place (in a place where you have the right to do so). You should better use an absolute path (instead of a relative one) for your HTML file name like that :
map.save(r"C:\Users\austi\Map1.html")
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
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()