pythonftpアップロードファイルとフォルダー

[ 参照リンク:Python FTP](https://www.imangodoc.com/32692.html)

ここで、session = session = ftplib.FTP(host =”、user =”、passwd =”)

def upload_dir(path_source, session, target_dir=None):
 files = os.listdir(path_source)
 # あなたがいた作業ディレクトリを覚えておいてください
 last_dir = os.path.abspath('.')
 # 次に、ターゲットの作業ディレクトリに切り替えます
 os.chdir(path_source)if target_dir:
  current_dir = session.pwd()try:
   session.mkd(target_dir)
  except Exception:
   pass
  finally:
   session.cwd(os.path.join(current_dir, target_dir))for file_name in files:
  current_dir = session.pwd()if os.path.isfile(path_source + r'/{}'.format(file_name)):upload_file(path_source, file_name, session)
  elif os.path.isdir(path_source + r'/{}'.format(file_name)):
   current_dir = session.pwd()try:
    session.mkd(file_name)
   except:
    pass
   session.cwd("%s/%s"%(current_dir, file_name))upload_dir(path_source + r'/{}'.format(file_name), session)
  # 以前のパスが変更された可能性があるため、前のパスに戻す必要があります
  session.cwd(current_dir)
 os.chdir(last_dir)
def upload_file(path, file_name, session, target_dir=None, callback=None):
 # 現在のftpパスを記録します
 cur_dir = session.pwd()if target_dir:try:
   session.mkd(target_dir)
  except:
   pass
  finally:
   session.cwd(os.path.join(cur_dir, target_dir))print("path:%s \r\n\t   file_name:%s"%(path, file_name))
 file =open(os.path.join(path, file_name),'rb')  # file to send
 session.storbinary('STOR %s'% file_name, file, callback = callback)  # send the file
 file.close()  # close file
 session.cwd(cur_dir)

Recommended Posts

pythonftpアップロードファイルとフォルダー
PythonはFTPを実装して、ファイルをループでアップロードします
Pythonはjsonファイルを読み書きします
Pythonはファイルの名前を変更します
Python and Go
pythonでファイルとディレクトリを削除する方法
Pythonでファイルを読み書きする方法
[python] ubuntuの下のpython2とpython3
Pythonの分解とパッケージ化
Python3の構成とentry.md
Pythonの自動操作とメンテナンス2
Pythonの紹介と環境のインストール
Pythonはクローラーとアンチクローラーを知っています
centos7はpython3とipythonをインストールします
ubuntu18.04python3.8をコンパイルしてインストールします
Pythonはファイルを1行ずつ読み取ります
Centos6.10はpythonとyumを再インストールします
Pythonオープン読み取りおよび書き込み
CentOS7はpython3とpip3をインストールします
Pythonトルネードアップロードファイル機能
Pythonの自動操作とメンテナンス1
Pythonのデータ構造とアルゴリズム
Pythonマルチプロセスおよびマルチスレッドの基本
PythonがFTP機能を実装する方法
CentOS6.9はpythonをコンパイルしてインストールします
pythonバッチ実行pyファイル
Pythonファイルを暗号化する3つの方法
CentOS6はpython3をコンパイルしてインストールします
Pythonのジェネレーターとイテレーター