Create missing directories in ftplib storbinary

后端 未结 6 854
傲寒
傲寒 2021-02-07 11:28

I was using pycurl to transfer files over ftp in python. I could create the missing directories automatically on my remote server using:

c.setopt(pycurl.FTP_CREA         


        
6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-07 11:48

    This code will create all missing folders in 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 will check all dirs in path and create missing dirs

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

提交回复
热议问题