美文网首页
使用pysmb 上传文件到共享

使用pysmb 上传文件到共享

作者: 贼噶人 | 来源:发表于2019-05-07 11:14 被阅读0次
  import os
from smb.SMBConnection import SMBConnection

from smb.smb_structs import OperationFailure

def deletefile(file):
    try:
        os.remove(file)
        print("%s删除成功!" % file)
    except PermissionError:
        deletefile(file=file)


if __name__ == '__main__':
    connect = SMBConnection(username='mytian', password='mytian'
                            , domain='mytian', remote_name='mytian', my_name='gang.zhou')
    connect.connect(ip='10.0.1.11')
    f = os.listdir('.')

    uploadOkFiles = []

    for item in f:
        if item.endswith(".apk"):
            splites = item.split('-')
            channelName = ''
            packageName = ''
            if -1 != splites[0].find('Rz'):
                packageName = 'Rz'
                channelName = splites[0].replace('Rz', '')
            if -1 != splites[0].find('Ma'):
                packageName = 'Ma'
                channelName = splites[0].replace('Ma', '')
            if -1 != splites[0].find('En'):
                packageName = 'En'
                channelName = splites[0].replace('En', '')
            if -1 != splites[0].find('Lo'):
                packageName = 'Lo'
                channelName = splites[0].replace('Lo', '')
            if -1 != splites[0].find('Ph'):
                packageName = 'Ph'
                channelName = splites[0].replace('Ph', '')
            if -1 != splites[0].find('Pb'):
                packageName = 'Pb'
                channelName = splites[0].replace('Pb', '')
            if -1 != splites[0].find('Xz'):
                packageName = 'Xz'
                channelName = splites[0].replace('Xz', '')
            if -1 != splites[0].find('St'):
                packageName = 'St'
                channelName = splites[0].replace('St', '')
            if -1 != splites[0].find('So'):
                packageName = 'So'
                channelName = splites[0].replace('So', '')
            if -1 != splites[0].find('Bk'):
                packageName = 'Bk'
                channelName = splites[0].replace('Bk', '')
            if -1 != splites[0].find('Py'):
                packageName = 'Py'
                channelName = splites[0].replace('Py', '')
            if -1 != splites[0].find('MaV'):
                packageName = 'MaV'
                channelName = splites[0].replace('MaV', '')
            if -1 != splites[0].find('LoV'):
                packageName = 'LoV'
                channelName = splites[0].replace('LoV', '')
            if -1 != splites[0].find('RzV'):
                packageName = 'RzV'
                channelName = splites[0].replace('RzV', '')
            if -1 != splites[0].find('EnV'):
                packageName = 'EnV'
                channelName = splites[0].replace('EnV', '')
            if -1 != splites[0].find('PyV'):
                packageName = 'PyV'
                channelName = splites[0].replace('PyV', '')
            if -1 != splites[0].find('Gx'):
                packageName = 'Gx'
                channelName = splites[0].replace('Gx', '')
            if -1 != splites[0].find('Ls'):
                packageName = 'Ls'
                channelName = splites[0].replace('Ls', '')
            typeP = 'Debug'
            if -1 != item.find('release'):
                typeP = 'Release'
            try:
                connect.createDirectory(service_name='version_release'
                                        , path='/NewMGarden/%s' % packageName)
            except OperationFailure:
                pass
            try:
                connect.createDirectory(service_name='version_release'
                                        , path='/NewMGarden/%s/%s' % (packageName, channelName))
            except OperationFailure:
                pass
            try:
                connect.createDirectory(service_name='version_release'
                                        , path='/NewMGarden/%s/%s/%s' % (packageName, channelName, typeP))
            except OperationFailure:
                pass
            try:
                connect.createDirectory(service_name='version_release'
                                        , path='/NewMGarden/%s/%s/%s/%s' % (packageName, channelName, typeP
                                                                            , 'V%s' % splites[splites.__len__() - 2]))
            except OperationFailure:
                pass

            try:
                connect.storeFile(service_name="version_release"
                                  , path='/NewMGarden/%s/%s/%s/%s/%s' % (packageName, channelName, typeP
                                                                         , 'V%s' % splites[splites.__len__() - 2], item)
                                  , file_obj=open(file=item, mode='rb'))
                print("%s上传成功!" % item)
                uploadOkFiles.append(item)
            except OperationFailure:
                pass
    connect.close()

    for item in uploadOkFiles:
        deletefile(item)

相关文章

网友评论

      本文标题:使用pysmb 上传文件到共享

      本文链接:https://www.haomeiwen.com/subject/hmfeoqtx.html