module filehelper.ftp_transfer

Inheritance diagram of pyquickhelper.filehelper.ftp_transfer

Short summary

module pyquickhelper.filehelper.ftp_transfer

provides some functionalities to upload file to a website

source on GitHub

Classes

class

truncated documentation

CannotCompleteWithoutNewLoginException

raised when a transfer is interrupted by a new login

CannotReturnToFolderException

raised when a transfer is interrupted by an exception and the class cannot return to the original folder

TransferFTP

this class uploads files to a website, if the remote does not exists, it creates it first

Properties

property

truncated documentation

Site

return the website

Methods

method

truncated documentation

__init__

constructor

_private_login

logs in

close

close the connection

cwd

go to a directory, if it does not exist, create it (if create is True)

dir

Lists the content of a path.

enumerate_ls

enumerate the content of a path

ls

list the content of a path

mkd

creates a directory

print_list

return the list of files in the current directory the function sends eveything to the logging function

pwd

Return the pathname of the current directory on the server.

retrieve

downloads a file

run_command

run a FTP command

transfer

transfers a file

Documentation

provides some functionalities to upload file to a website

source on GitHub

exception pyquickhelper.filehelper.ftp_transfer.CannotCompleteWithoutNewLoginException[source][source]

Bases: Exception

raised when a transfer is interrupted by a new login

source on GitHub

exception pyquickhelper.filehelper.ftp_transfer.CannotReturnToFolderException[source][source]

Bases: Exception

raised when a transfer is interrupted by an exception and the class cannot return to the original folder

source on GitHub

class pyquickhelper.filehelper.ftp_transfer.TransferFTP(site, login, password, fLOG=<function noLOG>)[source][source]

Bases: object

this class uploads files to a website, if the remote does not exists, it creates it first

Transfer files to webste through FTP

Simple sketch to transfer a list of files to a website through FTP

ftp = TransferFTP('ftp.<website>', alias, password, fLOG=print)

issues = [ ]
done = [ ]
notdone = [ ]
for file in files :

    try :
        r = ftp.transfer (file, path)
        if r : done.append( (file, path) )
        else : notdone.append ( (file, path) )
    except Exception as e :
        issues.append( (file, e) )

try :
    ftp.close()
except Exception as e :
    print ("unable to close FTP connection using ftp.close")

source on GitHub

constructor

Parameters
  • site – website

  • login – login

  • password – password

  • fLOG – logging function

source on GitHub

property Site[source]

return the website

source on GitHub

__init__(site, login, password, fLOG=<function noLOG>)[source][source]

constructor

Parameters
  • site – website

  • login – login

  • password – password

  • fLOG – logging function

source on GitHub

_private_login()[source][source]

logs in

source on GitHub

close()[source][source]

close the connection

source on GitHub

cwd(path, create=False)[source][source]

go to a directory, if it does not exist, create it (if create is True)

Parameters
  • path – path to the directory

  • create – True to create it

Returns

True or False

source on GitHub

dir(path='.')[source][source]

Lists the content of a path.

Parameters

path – path

Returns

list of path

See enumerate_ls

source on GitHub

enumerate_ls(path='.')[source][source]

enumerate the content of a path

Parameters

path – path

Returns

list of dictionaries

One dictionary:

{'name': 'www',
 'type': 'dir',
 'unique': 'aaaa',
 'unix.uid': '1111',
 'unix.mode': '111',
 'sizd': '5',
 'unix.gid': '000',
 'modify': '111111'}

source on GitHub

ls(path='.')[source][source]

list the content of a path

Parameters

path – path

Returns

list of path

see enumerate_ls

List files from FTP site

from pyquickhelper.filehelper import TransferFTP
ftp = TransferFTP("ftp....", "login", "password")
res = ftp.ls("path")
for v in res:
    print(v["name"])
ftp.close()

source on GitHub

mkd(path)[source][source]

creates a directory

Parameters

path – path to the directory

Returns

True or False

source on GitHub

print_list()[source][source]

return the list of files in the current directory the function sends eveything to the logging function

Returns

output of the command or True for success, False for failure

source on GitHub

pwd()[source][source]

Return the pathname of the current directory on the server.

Returns

pathname

source on GitHub

retrieve(fold, name, file, debug=False)[source][source]

downloads a file

Parameters
  • file – file name or stream (binary, BytesIO)

  • fold – full remote path

  • name – name of the stream on the website

  • debug – if True, displays more information

Returns

status

source on GitHub

run_command(command, *args)[source][source]

run a FTP command

Parameters
  • command – command

  • args – list of argument

Returns

output of the command or True for success, False for failure

source on GitHub

transfer(file, to, name, debug=False, blocksize=None, callback=None)[source][source]

transfers a file

Parameters
  • file – file name or stream (binary, BytesIO)

  • to – destination (a folder)

  • name – name of the stream on the website

  • debug – if True, displays more information

  • blocksize – see ftplib.FTP.storbinary

  • callback – see ftplib.FTP.storbinary

Returns

status

When an error happens, the original current directory is restored.

source on GitHub