Create missing directories in ftplib storbinary

I used pycurl to transfer files via ftp in python. I can create missing directories automatically on my remote server using:

c.setopt(pycurl.FTP_CREATE_MISSING_DIRS, 1) 

for some reason, I have to switch to ftplib. But I don’t know how here too. Is there any way to add to the stored function for this? or do I need to create directories manually?

+8
python pycurl ftplib
source share
4 answers

FTP_CREATE_MISSING_DIRS - curling operation ( added here ). I would venture to suggest that you need to do this manually using ftplib, but I would like someone to be wrong?

I would do something like the following: (untested, and need to catch ftplib.all_errors )

 ftp = ... # Create connection # Change directories - create if it doesn't exist def chdir(dir): if directory_exists(dir) is False: # (or negate, whatever you prefer for readability) ftp.mkd(dir) ftp.cwd(dir) # Check if directory exists (in current location) def directory_exists(dir): filelist = [] ftp.retrlines('LIST',filelist.append) for f in filelist: if f.split()[-1] == dir and f.upper().startswith('D'): return True return False 

Or you could do directory_exists as follows: (a little more difficult to read?)

 # Check if directory exists (in current location) def directory_exists(dir): filelist = [] ftp.retrlines('LIST',filelist.append) return any(f.split()[-1] == dir and f.upper().startswith('D') for f in filelist) 
+9
source share

I know this is kind of an old post, but I just needed it and came up with a very simple function. I am new to Python, so I would be grateful for any feedback.

 from ftplib import FTP ftp = FTP('domain.com', 'username', 'password') def cdTree(currentDir): if currentDir != "": try: ftp.cwd(currentDir) except IOError: cdTree("/".join(currentDir.split("/")[:-1])) ftp.mkd(currentDir) ftp.cwd(currentDir) 

Usage example:

 cdTree("/this/is/an/example") 
+6
source share

I tried to add this as a comment on @Alex L's answer, but it was too long. You need to go down with recursion when changing the directory if you want to create directories along the way. For example.

 def chdir(ftp, directory): ch_dir_rec(ftp,directory.split('/')) # Check if directory exists (in current location) def directory_exists(ftp, directory): filelist = [] ftp.retrlines('LIST',filelist.append) for f in filelist: if f.split()[-1] == directory and f.upper().startswith('D'): return True return False def ch_dir_rec(ftp, descending_path_split): if len(descending_path_split) == 0: return next_level_directory = descending_path_split.pop(0) if not directory_exists(ftp,next_level_directory): ftp.mkd(next_level_directory) ftp.cwd(next_level_directory) ch_dir_rec(ftp,descending_path_split) 
+4
source share

This code will create all the missing folders in the path:

 ... def chdir(ftp_path, ftp_conn): dirs = [d for d in ftp_path.split('/') if d != ''] for p in dirs: print(p) check_dir(p, ftp_conn) def check_dir(dir, ftp_conn): filelist = [] ftp_conn.retrlines('LIST', filelist.append) found = False for f in filelist: if f.split()[-1] == dir and f.lower().startswith('d'): found = True if not found: ftp_conn.mkd(dir) ftp_conn.cwd(dir) if __name__ == '__main__': ftp_conn = ... # ftp connection t = 'FTP/for_Vadim/1/2/3/' chdir(t, ftp_conn) 

This code checks all paths in the path and creates missing dirs

before "FTP / for_Vadim /" after "FTP / for_Vadim / 1/2/3 /"

0
source share

All Articles