Permission Error while Downloading from Mega.py

Viewed 363

Download File from Mega using mega.py

I'm trying to download a file from my Megadrive using mega.py module

while running the code as specified in mega module home url

I'm getting a Permission error as specified below

Code:

from mega import Mega


mega = Mega()
email ="example@example.com"
password ="xxxxxxxxxx"
m = mega.login(email, password)

filename = "filename.jpg"

file = m.find(filename)
m.download(file)

Error:

Traceback (most recent call last):
  File "C:\Users\xxxx\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 806, in move
    os.rename(src, real_dst)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\AppData\\Local\\Temp\\megapy_d6ozyjm' -> 'filename.jpg'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Desktop\Mega.py", line 14, in <module>
    m.download(file)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\mega\mega.py", line 564, in download
    return self._download_file(file_handle=None,
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\site-packages\mega\mega.py", line 745, in _download_file
    shutil.move(temp_output_file.name, output_path)
  File "C:\Users\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 821, in move
    os.unlink(src)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\\Users\\\AppData\\Local\\Temp\\megapy_d6ozyjm'
2 Answers

I'm new in stackoverflow, I don't know and how to mark duplicated questions or things like that. Just want to help because I had that problem.

This is a duplicated question that already has a right answer: https://stackoverflow.com/a/68465437/14104956

To put it faster, here's the answer, hope it helps you:

find the file path with pip show mega.py

Open the file

Goto line 745 where the line shutil.move(temp_output_file.name, output_path) is.

Add temp_output_file.close() right above it.

Save & try again.

You can just use try/except to handle that:

try:
    m.download(file)
except PermissionError:
    pass

this works with multiple files, loops, etc.

Related