[ 参照リンク: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