Create missing directories in ftplib storbinary

后端 未结 6 853
傲寒
傲寒 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条回答
  •  再見小時候
    2021-02-07 11:49

    I know it's kind of an old post but I just needed this and came up with a very simple function. I'm new to Python so I'd appreciate 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")
    

提交回复
热议问题