2

I'm looking for the option to create a year / month / day folder structure with the Python ftplib module

Connection:

ftp = ftplib.FTP(ftp_servidor, ftp_usuario, ftp_clave)

Loading variables:

ftp_raiz = 'TEST/'
filename = '2019-10-01T00-00-00.txt'

Function:

def cdTree(ftp, filename=None, path=None):

    if filename is not None:
        date = datetime.datetime.strptime(filename, '%Y-%m-%dT%H-%M-%S.txt')
        path = ftp_raiz + date.strftime('%Y') + '/' + date.strftime('%m') + '/' + date.strftime('%d')
        print filename
    if path != "":
        try:
            ftp.cwd(path)
        except error_perm as e:
            print e, ", creating folder"
            print path
            cdTree(ftp, path="/".join(path.split("/")[:-1]))
            ftp.mkd(path)
            ftp.cwd(path)

cdTree(ftp, filename, 'TEST')

Finally I do not create the folder structure, and it throws me the following error:

2018-10-18T00-00-00.txt
550 Failed to change directory. , creating folder
2018/10/18
550 Failed to change directory. , creating folder
2018/10
Traceback (most recent call last):
  File "ftp2.py", line 34, in <module>
    cdTree(ftp, filename)
  File "ftp2.py", line 30, in cdTree
    cdTree(ftp, path="/".join(path.split("/")[:-1]) )
  File "ftp2.py", line 31, in cdTree
    ftp.mkd(path)
  File "C:\python27\lib\ftplib.py", line 589, in mkd
    resp = self.sendcmd('MKD ' + dirname)
  File "C:\python27\lib\ftplib.py", line 251, in sendcmd
    return self.getresp()
  File "C:\python27\lib\ftplib.py", line 226, in getresp
    raise error_perm, resp
ftplib.error_perm: 550 Create directory operation failed.

Note: perform the test of creating only one folder and it works!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Alejandro
  • 21
  • 6

2 Answers2

2

Your code does not make much sense to me.

I believe you wanted this:

def cdTree(ftp, path):
    print "entering folder {0}".format(path)
    try:
        ftp.cwd(path)
    except:
        print "failed to enter, creating"
        cdTree(ftp, path="/".join(path.split("/")[:-1]))
        ftp.mkd(path)
        ftp.cwd(path)

ftp_raiz = 'TEST/'
filename = '2019-10-01T00-00-00.txt'

date = datetime.datetime.strptime(filename, '%Y-%m-%dT%H-%M-%S.txt')
path = ftp_raiz + date.strftime('%Y/%m/%d')

cdTree(ftp, path)
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
1

I am not sure if this helps but tried the same on HDFS file system and it helped me solve my requirement.One can change the required output in the function generateDateFromThisYear()

import datetime as d
import subprocess

def run_cmd(args_list):
    """
    run linux commands
    """
    # import subprocess
    print('Running system command: {0}'.format(' '.join(args_list)))
    proc = subprocess.Popen(args_list, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    s_output, s_err = proc.communicate()
    s_return = proc.returncode
    return s_return, s_output, s_err

def hdfsMakedir(hdfs_file_path):
    (ret, out, err) = run_cmd(['hdfs', 'dfs', '-mkdir','-p', hdfs_file_path])



def make_necessary_dirs(dir_to_create: str):
    # print(pathExists(dir_to_create))
    if pathExists(dir_to_create) == 1:
        hdfsMakedir(dir_to_create)
        return dir_to_create
    else:
        return "the folder already exists"


list_of_dates =[]

def generateDateFromThisYear(year) :    

    months = [1,2,3,4,5,6,7,8,9,10,11,12]
    dt = "" #str(d.date(2012, 1, 1))
    for month in months:
        no_of_days = monthrange(year, month)[1] # Taking only second Value as it returns number of weeks and no of days 
        for day in range(1,no_of_days+1) :
            x = d.date(year,month,day)
            y= x.strftime("%Y")+'/'+x.strftime("%m")+'/'+x.strftime("%d")
            list_of_dates.append(y)

    for i in list_of_dates:
        make_necessary_dirs(i)
Sumeet Kumar
  • 123
  • 6