How to use FTP with Python


I had a chance to use ftp with Python, so I'll leave it as a note.

FTP connection

import ftplib

ftp = ftplib.FTP(IP_ADDRESS)
ftp.login(USER, PASSWORD)

File upload (text)

with open("a.txt", "rb") as f:
    ftp.storlines("STOR /aa.txt", f)

File upload (binary)

with open("", "rb") as f:
    ftp.storbinary("STOR /", f)

File download (text)

with open("b.txt", "w") as f:
    ftp.retrlines("RETR /aa.txt", f.write)

File download (binary)

with open("", "wb") as f:
    ftp.retrbinary("RETR /", f.write)

If you don't use with, it looks like this

#Binary is rb, text is r
f = open(filename, 'rb') 
ftp.storbinary('STOR {}'.format(PATH), f)

Directory creation


Get a list of files

file_list = ftp.nlst(".")

Error handling

    # process
except ftplib.all_errors as e:
        print('FTP Error :', e)


-[Python] Run FTP from Python

Recommended Posts

How to use FTP with Python
Python: How to use async with
python3: How to use bottle (2)
[Python] How to use list 1
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to use tkinter with python in pyenv
[Python] How to use Pandas Series
How to use Requests (Python Library)
How to use SQLite in Python
How to use virtualenv with PowerShell
How to use python interactive mode with git bash
How to get started with Python
[Python] How to use list 3 Added
How to use Mysql in python
Python: How to use pydub (playback)
How to use PubChem in Python
How to calculate date with python
How to use python zip function
[Python] How to use Typetalk API
How to use Realsense's Python wrapper PyRealsense with Jetson Nano
[Python] Summary of how to use pandas
[Introduction to Python] How to use class in Python?
How to install and use pandas_datareader [Python]
How to use ManyToManyField with Django's Admin
How to use OpenVPN with Ubuntu 18.04.3 LTS
How to use Cmder with PyCharm (Windows)
How to work with BigQuery in Python
[Introduction to Python] Let's use foreach with Python
[Python] How to use import sys sys.argv
How to use Ass / Alembic with HtoA
How to do portmanteau test with python
[Python] Organizing how to use for statements
Memorandum on how to use gremlin python
[Python2.7] Summary of how to use unittest
How to use jupyter notebook with ABCI
python: How to use locals () and globals ()
How to use __slots__ in Python class
How to use CUT command (with sample)
How to use "deque" for Python data
How to enter Japanese with Python curses
How to use Python zip and enumerate
[Python] Understand how to use recursive functions
Summary of how to use Python list
How to use regular expressions in Python
[Python] How to deal with module errors
[Python2.7] Summary of how to use subprocess
How to use is and == in Python
[Blender x Python] How to use modifiers
How to use SQLAlchemy / Connect with aiomysql
How to install python3 with docker centos
[Question] How to use plot_surface of python
How to use JDBC driver with Redash
How to use xml.etree.ElementTree