python3 ftp模块_python3 写的一个通过ftp远程批量处理的工具

直接上代码:ftpFun.py

#coding=utf-8

import ftplib

import os

import os.path

class ftpInterface:

def __init__(self, host = '', port = 21, user = '', pwd = '', timeout = 5):

self.host = host

self.port = port

self.user = user

self.pwd = pwd

self.timeout = timeout

self.filesParentList = []

self.connect()

self.ftp.encoding = 'GB18030'

#ls = self.ftp.dir()

#print(ls)

def setlocalPath(self, ftpEditPath):

#self.ftpPath = ftpEditPath

#self.ftpPathSetting('')

workPath = self.ftp.pwd()

self.ftp.cwd(workPath)

def connect(self):

try:

self.ftp = ftplib.FTP()

self.ftp.connect(self.host, self.port, self.timeout)

except Exception as connectEx:

print(connectEx)

self.login()

def close(self):

try:

self.ftp.quit()

except Exception as closeEx:

print(closeEx)

def login(self):

try:

self.ftp.login(self.user, self.pwd)

except Exception as loginEx:

print(loginEx)

def ftpPathSetting(self, SettingPath):

try:

#substr = self.filesParentList[0]

#pos = SettingPath.find(substr)

#tmpPath = SettingPath[pos + len(substr):]

#allPath = self.ftpPath + tmpPath

#self.ftp.cwd(allPath)

print(self.ftpPath)

print(self.ftp.pwd())

l = self.ftp.nlst()

print(l)

self.ftp.cwd(self.ftpPath)

except Exception as ex:

print(ex)

def ftpUpLoadFile(self, folderPath):

upFileMap = self.getFileNames(folderPath)

if(not upFileMap):

return -3

try:

isDirExist = False

for fileKey in upFileMap.keys():

'''find folderName == '1' pos '''

filePath = self.getFtpFilePath(upFileMap[fileKey])

if(filePath == ''):

continue

else:

isDirExist = True

ftpDir = os.path.dirname(filePath)

print(ftpDir)

self.createFtpDir(ftpDir)

self.ftp.storbinary('STOR ' + filePath , open(upFileMap[fileKey], 'rb'))

if(isDirExist == False):

return -1

except Exception as ex:

print(ex)

return -2

self.close()

return 0

def getFileNames(self, folderPath):

if(not os.path.isdir(folderPath)):

return False

fileNameMap = {}

for parent, dirnames, filenames in os.walk(folderPath):

self.filesParentList.append(parent)

for filename in filenames:

pathName = os.path.join(parent, filename)

print(pathName)

fileNameMap[filename] = pathName

return fileNameMap

def ftpRemoveFile(self, filePath):

try:

path = self.getFtpFilePath(filePath)

print(path)

if(path == ''):

return -1

self.ftp.delete(path)

except Exception as ex:

return -2

print(ex)

self.close()

def ftpRenameFile(self, beforName, afterName):

try:

path = self.getFtpFilePath(beforName)

print(path)

if(path == ''):

return -1

self.ftp.rename(beforName, afterName) #'/bbs/test.doc'

except Exception as ex:

return -2

print(ex)

self.close()

def getFtpFilePath(self, srcPath):

pos1 = srcPath.find('//voicePrompt//1')

pos2 = srcPath.find('\\voicePrompt\\1')

pos3 = srcPath.find('\voicePrompt\1')

pos4 = srcPath.find('/voicePrompt/1')

if(pos1 == -1 and pos2 == -1 and pos3 == -1 and pos4 == -1):

return ''

pos = 0

posList = [pos1, pos2, pos3, pos4]

for p in posList:

if(p != -1):

pos = p

break

filePath = srcPath[pos + len("/voicePrompt/1"):]

return filePath

def createFtpDir(self, ftpPath):

Paths = ftpPath.split('\\')

print(Paths)

tmpPath = ''

for path in Paths:

if(path == ''):

continue

tmpPath += '/' + path

try:

print(tmpPath)

self.ftp.mkd(tmpPath)

except Exception as ex:

print(ex)  无法放下界面处理的代码,在下一篇文章中上传

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值