My requirement is to download the files which are arrived in the root directory in the last 24 hours.
The code below is working in a subdirectory (let's say ftp.cwd("/Landing/") but trigger the error while I am changing to the root directory.
filematch='*.csv'
ftp.cwd("/")
for file_data in ftp.mlsd(filematch):
file_name,meta = file_data
last_modified = datetime.strptime(meta.get("modify"), "%Y%m%d%H%M%S")
if (last_modified) >= now- timedelta(hours=0, minutes=1440):
print(last_modified)
local_filename = os.path.join('C:\\Work\\', file_name)
file = open(local_filename, 'wb')
with open(local_filename, "wb") as file:
ftp.retrbinary(f"RETR {file_name}", file.write)
If I'am changing the >=
to ==
then its working in root directory ( but both condition are fine in subdirectory; issue is only in root)
if (last_modified) = now- timedelta(hours=0, minutes=1440):
Error Message:-
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 425, in retrbinary with self.transfercmd(cmd, rest) as conn: File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 382, in transfercmd return self.ntransfercmd(cmd, rest)[0] File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 348, in ntransfercmd resp = self.sendcmd(cmd) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 275, in sendcmd return self.getresp() File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.1776.0_x64__qbz5n2kfra8p0\lib\ftplib.py", line 248, in getresp raise error_perm(resp)ftplib.error_perm: 550 Permission denied.