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!)
Related
I need help on the issue I am having. "PermissionError: [WinError 32] The process cannot access the file because it is being used by another process:".
So the script below deletes a folder with an excel file in it. However, if the excel file is opened, it doesn't proceed on the shutil.rmtree(dirpath) command. Can somebody lead me to a solution where user will be prompt when the file is opened? Looking forward to your feedback. Thank you very much in advance.
import os
import shutil
dirpath = os.path.join('C:/Path/Folder', 'Folder')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
shutil.rmtree(dirpath)
print('Deleted.')
else:
print('Folder does not exist!')
messagebox.showinfo('Ok.')
Given that you are using Windows, I would say try the following:
import os
import shutil
dirpath = os.path.join('C:/Path/Folder', 'Folder')
if os.path.exists(dirpath) and os.path.isdir(dirpath):
try:
os.rename(dirpath, dirpath)
shutil.rmtree(dirpath)
print('Deleted.')
except:
messagebox.showinfo('File opened by another process')
else:
print('Folder does not exist!')
messagebox.showinfo('Ok.')
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'
I'm trying to get watchdog setup to feed new files into the dcmread function but can't seem to get past a PermissionError.
print("Watchdog received created event - % s." % event.src_path)
home = r'C:\Users\name\PycharmProjects\TEST_DCM_DIR'
fn = os.path.basename(event.src_path)
print(fn)
dcmpath = glob.glob(home + '\\' + fn)
print(dcmpath[0])
dcmpath2 = r'C:\Users\name\PycharmProjects\TEST_DCM_DIR\test.dcm'
print(dcmpath2)
ds = dcmread(dcmpath[0])
Both the hard coded and the variable paths print the same file path, but when I use the variable I get the following errors.
File "C:\Users\name\PycharmProjects\venv\lib\site-packages\pydicom\filereader.py", line 861, in dcmread
fp = open(fp, 'rb')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\name\\PycharmProjects\\TEST_DCM_DIR\\test.dcm'
The code works fine when I pass through the hard-coded path which makes me think it isn't a permission thing (from searching stackoverflow). I'm hoping it's something to do with how I'm compiling the path. I've tried os.path.join methods (dcmpath = os.path.join(home, fn)) and using the event.src_path handle too and haven't managed to crack it. Any insight would be greatly appreciated. I'm running this code on a Windows machine.
edit: What appeared to be the problem was that the file was being read before it was released from copying. Adding a time.sleep(2) line before passing the path to dcmread solved this problem.
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/'
When i want to open a file in C:\ (windows dir.) this error shows up:
PermissionError: [Errno 13] Permission denied: 'C:\h.txt'
What should i do?
I know this question has been asked several times but i can't find solution!
code:
f=open ('C:\\h.txt','w')
f.write ('python')
f.close
I am not on win machine, but give this a try, you can manage permissions using these commands
Try to open your file using os.fdopen
import os
with os.fdopen(os.open('file.txt', os.O_WRONLY | os.O_CREAT, 0600), 'w') as f:
f.write(...)
UPDATE
import os
is_accessible = os.access("C:\\temp\\python",os.F_OK) #Check if you have access, this should be a path
if is_accessible == False: #If you don't, create the path
os.makedirs("C:\\temp\\python")
os.chdir("C:\\temp\\python") # Check now if the path exist
f = os.open( "p.txt", os.O_RDWR|os.O_CREAT ) #Create the file
os.write(f, b"This is a test \n") #Try to write
os.close(f)
I'm not on a Windows machine but perhaps you should try and create this file in the directory c:\Temp.
Likewise make sure you've not got Notepad etc with that file open.