0

I'd like to know how to download all data from a ftp dir to a folder in my operation system, but when I try to use a folder/path as a writible object python returns me an exception of permission diened.

My trial:

# os.chdir(self.dir_data_base + '\\Thrash')
# try:
#     load_workbook('relatorio.xlsx').save('relatorio.xlsx')
# except:
#     messagebox.showerror('Erro','Você não pode realizar qualquer operação com um dos seguintes arquivos abertos: relatorio.xlsx, Historico_de_lançamentos.xlsx')
#     return False
# try:
#     load_workbook('Historico_de_lançamentos.xlsx').save('Historico_de_lançamentos.xlsx')
# except:
#     messagebox.showerror('Erro','Você não pode realizar qualquer operação com um dos seguintes arquivos abertos: relatorio.xlsx, Historico_de_lançamentos.xlsx')
#     return False
# os.chdir(self.dir_data_base)
# ftp = ftplib.FTP(timeout=30)
# ftp.connect('any.com.br')
# ftp.login('username','pass')
# backup_name = self.aux_function_erase_blank_space(self.restore.get())
# ftp.cwd('/Banco de dados/' + backup_name)
# filenames = ftp.nlst() # get filenames within the directory
# for filename in filenames:
#     local_filename = self.dir_data_base
#     file = open(local_filename, 'wb')
#     ftp.retrbinary('RETR '+ filename, file.write)
#     file.close()
# ftp.quit() # This is the “polite” way to close a connection

PS: the original code is into a big project compacted into a class, so you see self

  • does this link answer your question? https://stackoverflow.com/questions/11910579/ioerror-errno-13-permission-denied-ftplib – Ali Massoud May 19 '23 at 06:52
  • What's in `self.dir_data_base`? If it's a directory, obviously you can't overwrite that. Try `os.path.join(self.dir_data_base, filename)`, or just `filename` if you want to save into the current directory. Perhaps see also [What exactly is current working directory?](https://stackoverflow.com/questions/45591428/what-exactly-is-current-working-directory/66860904) – tripleee May 19 '23 at 07:03
  • The duplicate has a specific answer about this case; https://stackoverflow.com/a/40680058/874188 – tripleee May 19 '23 at 07:06

1 Answers1

2

You're trying to open the directory self.dir_data_base as a file for writing, therefore getting a permission denied error. You should open a file instead under that directory.

Change:

local_filename = self.dir_data_base
file = open(local_filename, 'wb')

to:

file = open(os.path.join(self.dir_data_base, filename), 'wb')
blhsing
  • 91,368
  • 6
  • 71
  • 106