@@ -16,6 +16,7 @@ def __init__(
1616 username : str ,
1717 password : str ,
1818 local_dir : str = "./" ,
19+ timeout : int = 30 ,
1920 * args ,
2021 ** kwargs ,
2122 ):
@@ -28,6 +29,7 @@ def __init__(
2829 username (str): Username for FTP authentication.
2930 password (str): Password for FTP authentication.
3031 local_dir (str, optional): Local directory to save the retrieved file. Defaults to "./".
32+ timeout (int, optional): Timeout value for FTP connection measued in seconds. Defaults to 30 seconds.
3133 *args: Variable length argument list.
3234 **kwargs: Arbitrary keyword arguments.
3335 """
@@ -37,6 +39,7 @@ def __init__(
3739 self .username = username
3840 self .password = password
3941 self .local_dir = local_dir
42+ self .timeout = timeout
4043
4144 def run (self ) -> Result :
4245 """
@@ -55,16 +58,16 @@ def run(self) -> Result:
5558
5659 """
5760 try :
58- ftp = FTP (self .ftp_url , timeout = 30 ) # Modify timeout as needed
61+ if not os .path .isdir (self .local_dir ):
62+ os .makedirs (self .local_dir , exist_ok = True )
63+
64+ ftp = FTP (self .ftp_url , timeout = self .timeout ) # Modify timeout as needed
5965 ftp .login (user = self .username , passwd = self .password )
6066
6167 remote_dir , remote_filename = os .path .split (self .ftp_remote_filepath )
6268 if remote_dir :
6369 ftp .cwd (remote_dir )
6470
65- if not os .path .isdir (self .local_dir ):
66- return Failure (f"Local directory does not exist: { self .local_dir } " )
67-
6871 local_filepath = os .path .join (self .local_dir , remote_filename )
6972 with open (local_filepath , "wb" ) as f :
7073 ftp .retrbinary (f"RETR { remote_filename } " , f .write )
0 commit comments