Could someone please give me some guidance. I am pretty fresh with python.
All I am wanting to do is download zip files from web addresses and save them to a folder.
The same process happens every month so I am trying to automate it.
I have sucessfully download the zip file to a folder of my choice, but when I get python to create the folder I get permission errors. I have read online I can use os.chmod and grant permissions but I cannot figure out how to structure it/ write it so it works
This is what I have so far.
import requests, zipfile, io
import os
from datetime import datetime
d = datetime.today().strftime('%b%y') #'Dec20'
newpath = 'L:/Support/Data_load/NativeTitle/{}'.format(d)
if not os.path.exists(newpath):
os.makedirs(newpath)
os.chmod(newpath , 0o0777)
#chmod -R 777 'L:/Support/Data_load/NativeTitle/{}'.format(d)
print('Folder created in L:\Support\Data_load\NativeTitle')
print('Beginning file download with urllib2...')
url = 'http://www.nntt.gov.au/GeoDocs//ESRI/NTDA_Schedule_Nat_shp.zip'
urllib.urlretrieve(url, newpath)
IOError: [Errno 13] Permission denied: 'L:/Support/Data_load/NativeTitle/Oct21'
Related
Issue: Unable to save file in directory (/root/Notion/Image) when using Cron schedule
This is what my code is trying to do:
Check email
Download image attachment
Store in a directory - root/Notion/Image
Retrieve file path
The script is working when I run it manually in Google Cloud terminal. The problem is when I try to schedule it on Cron, it's unable to access the folder to save the file locally.
This is the error when the script failed and require permission:
Traceback (most recent call last):
File "Notion/test.py", line 121, in <module>
path = get_attachments(email.message_from_bytes(msg[0][1]))
File "Notion/test.py", line 47, in get_attachments
with open(filePath, 'wb') as f:
PermissionError: [Errno 13] Permission denied: '/root/Notion/Image/3.jpeg'
This is the code to retrieve attachment from email
def get_attachments(msg):
for part in msg.walk():
if part.get_content_maintype()=='multipart':
continue
if part.get('Content-Disposition') is None:
continue
fileName = part.get_filename()
if bool(fileName):
filePath = os.path.join(attachment_dir, fileName)
with open(filePath, 'wb') as f:
f.write(part.get_payload(decode=True))
return str(filePath)
Resolved:
The problem is that I shouldn't use root directory since it requires permission. I've changed it to home directory instead.
attachment_dir = '/home/dev_thomas_yang/folder_name/folder_name'
For people who needs to check their home direction, simply run this script.
from pathlib import Path
home= str(Path.home())
print(home)
Thanks Triplee for the patience to breakdown my issue despite my sloppy ways of presenting it!
The easiest fix hands down is to change the code so it doesn't try to write to /root. Have it write to the invoking user's home directory instead.
Your question doesn't show the relevant parts of the code, but just change attachment_dir so it's not an absolute path. Maybe separately take care of creating the directory if it doesn't already exist.
import pathlib
# ...
attachment_dir = pathlib.Path("cron/whatever/attachments").mkdir(parents=True, exist_ok=True)
# ...
for loop in circumstances:
get_attachments(something)
A better design altogether would be to have get_attachments accept the directory name as a parameter, so you can make this configurable from the code which calls it. Global variables are a nuisance and cause hard-to-debug problems because they hide information which is important for understanding the code, and tricky to change when you try to debug that code and don't know which parts of the code depend on the old value.
I want to move a file to another folder, but my permissions seems to be denied.
I have tried googling the problem, and although this is probably a pretty simple problem, I am not able to find solutions. I am the administrator on the system.
import os
import shutil
import re
textregex = re.compile(r'(.*).txt')
folder_files = os.listdir('D:\\progetti python\\renamedates.py\\dates')
for filename in folder_files:
txtfiles = textregex.findall(filename)
print('found some text files: ' + filename)
for file in txtfiles:
shutil.copy(f'{file}','D:\\progettipython\\renamedates.py\\newfolder')
I expect to copy my file (which is a textfile) without having my copy denied.
I can give you an example how to copy just .txt from folder a to folder b:
using glob to easy check for textfile ext.
import shutil
import glob
textfiles = glob.glob(r'C:\foldera\*.txt')
for filename in textfiles:
shutil.copy(f'{filename}',r'C:\folderb')
I have a folder of txt folders that I want to import into python as a variable. Ideally, I want a variable 'profession_texts' where each txt file is an element in a list. This is what I have at the moment:
import os
profession_folder_path = '../fp/Updated/Profession/'
profession_files = os.listdir(profession_folder_path)
profession_texts = [open(profession_folder_path+file_name, encoding='utf-8').read() for file_name in profession_files]
print(profession_texts[0])
Yet, when running this script, I get the error:
PermissionError: [Errno 13] Permission denied: '../fp/Updated/Profession/Athlete'
So I have two problems. How do I get rid of this PermissionError? Once this error is resolved, will my code work for attaining my goal?
You no need to append file name with directory as (profession_folder_path+file_name). Use os.path.realpath(file_name) instead
import os
profession_folder_path = '../fp/Updated/Profession/'
profession_files = os.listdir(profession_folder_path)
profession_texts = [open(os.path.realpath(file_name)).read() for file_name in profession_files]
print(profession_texts[0])
and for permissions you need to have read permissions on file and execute permission on directory if you are using unix. Run below command:
chmod -R a+rx '../fp/Updated/Profession/'
I am working with django and celery. In my celery task, I instantiate a class and that class is responsible for generating and mailing a CSV file.
My problem is I am getting IOError: [Errno 13] Permission denied
when i try to do fp = open(filename, 'w'). But how do I get to know which user of my server is trying to create that file and how can I provide that user with appropriate permissions.I am working on AWS server.
My code for writing files is this:
with open(filename, 'w') as f_pointer:
os.chmod(filename, 777)
myfile = csv.writer(f_pointer)
myfile.writerow(columns)
myfile.writerows(rows)
Thanks
First check the file permission or owner , then use the property permission.
ls -l filename
Another, you should check if this user can create the filename in the directory .
The file is opened by the same user who is running the process. Now if the directory where you are creating the file or file you opening to write to are not writable by the user, then chmod will not work.
You'll have to chose the directory/file that is writable by the user.
What I am trying to do, is take a .mp3 file from a folder within a folder. Multiple times.
Everything works perfect, except for when I try to copy file to a new folder (which already exists), and it gives: [Errno 13] Permission denied:
import os, shutil
def startup():
os.system("cls")
print "\n osu! Extractor."
print "\n Press ENTER to begin."
raw_input()
for basename in os.listdir('C:\Program Files (x86)\osu!\Songs'):
basename2 = ('C:\Program Files (x86)\osu!\Songs\\' + basename)
for song in os.listdir(basename2):
if song.endswith('.mp3'):
print song
raw_input()
shutil.copy(basename2, 'C:\Program Files (x86)\osu!\Extracted_Songs')
if __name__ == '__main__':
startup()
I know that I have access to copy it manually, and I am admin on my computer, so I have no clue why this is happening. Help would be greatly appreciated.
By default, programs are not given permission to write into Program Files and Program Files (x86). Save the file somewhere else or grant your program the permissions (e.g. by running it as administrator).
(shoutouts to Osu!)