简体   繁体   中英

PermissionError: [WinError 5] Access is denied encountered while running in python

import glob
import os
import shutil




for name in os.listdir('C:/Users/Ajay/Downloads'):
    if name.endswith('.xlsx') or name.endswith('.csv'):
        os.rename('C:/Users/Ajay/Downloads', 'C:/Users/Ajay/Projects/GFD')
        print(name)





Traceback (most recent call last):
  File "C:\Users\Ajay\Projects\Desktop\automation.py", line 33, in <module>
    os.rename('C:/Users/Ajay/Downloads', 'C:/Users/Ajay/Projects/GFD')
PermissionError: [WinError 5] Access is denied: 'C:/Users/Ajay/Downloads' -> 'C:/Users/Ajay/Projects/GFD'

I have tried to use shutil.move but I get permission denied. I have all the access to the directory as I am the admin, not sure why the issue still comes up.

That code would not move any files, but instead attempt to rename the directory every time. As you indicated, you can use the shutil.move() method to do what you intend.

The error is the same on both operations, therefore although you might have enough permission, you are probably executing the application from a non-privileged environment. If you are the system administrator, this could usually be solved by running the program as an administrator:

right click, run as administrator (directly on the python executable)

or

Search for CMD, right click, run as administrator, execute python (application name)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM