Skip to content

Instantly share code, notes, and snippets.

@minustore
Last active December 19, 2015 08:59
Show Gist options
  • Save minustore/a6fde8d9bb0edbcf9538 to your computer and use it in GitHub Desktop.
Save minustore/a6fde8d9bb0edbcf9538 to your computer and use it in GitHub Desktop.
simple upload all files in current dir
# -*- coding: utf-8 -*-
import os
import ftplib
#uploadList = sys.argv[1:]
ipaddress = "xx.xx.xx"
port = "21"
username = "username"
passwd = "passwd"
def uploadfile(filepath,filename):
file_handle=open(filepath,"rb")
ftp.storbinary('STOR '+ filename, file_handle)
def getFiles(myPath):
'''get list of files with an optional (list of) extension(s) from a folder'''
allFiles = []
allFilenames = []
l = os.listdir(myPath)
''' if myPath == './' change it to abs path '''
if myPath == "./":
myPath = os.getcwd()
for myFileName in l:
# don't upload itself
if myFileName == "ftpupload.py":
continue
filepath = os.path.join(myPath, myFileName)
# only get files,don't get dir
if os.path.isfile(filepath):
allFiles.append(myFileName)
return allFiles
ftp = ftplib.FTP()
ftp.set_debuglevel(2)
ftp.connect(ipaddress, port)
ftp.login(username,passwd)
print ftp.getwelcome()
files = getFiles('./')
for onefile in files:
uploadfile(onefile,onefile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment