从FTP Python获取特殊字符文件名

时间:2018-07-16 02:58:01

标签: python ftp

请支持从FTP下载文件,该文件名包含特殊字符,如下所示:

def Getting_SPO():

    ftp = FTP(ip)
    ftp.login('nmcman',password)
    #print "File List:" 
    #files = ftp.dir()
    directory ="/opt/mv36/core/nmc/hist/raw" #dir i want to download files from, can be changed or left for user    input
    filematch = "*2018_07_15_2200_NE:15066.CAI2699-TN1_CAI2387-TN1_1.all.cdb*"# a match for any file in this case, can be changed or left for user to input
    ftp.cwd(directory)
    for Spo_file in ftp.nlst(filematch): # Loop - looking for matching files    
        print(ftp.nlst(filematch))
        fhandle = open(Spo_file, 'wb'), sys.Text.Encoding.Unicode
        print( ' \n Getting ' + Spo_file )#for confort sake, shows the file that's being retrieved
        ftp.retrbinary('RETR ' + Spo_file, fhandle.write)

        fhandle.close()
        return Spo_file
filename_MMU2C=Getting_SPO()

1 个答案:

答案 0 :(得分:0)

以下解决了:

def Getting_SPO():
  ftp = FTP(ip)
  ftp.login('nmcman',password)
  directory ="/opt/mv36/core/nmc/hist/raw" #dir i want to download files from, can be changed or left for user    input
  filematch = "*NE:12057.HUB0012-5GATH-SPO1_1_1*"# a match for any file in this case, can be changed or left for user to input
  ftp.cwd(directory)
  for Spo_file in ftp.nlst(filematch): # Loop - looking for matching files    
    x =ftp.nlst(filematch)
    x=(x[0].replace(":", "_"), 'wb')
    print(x)

    global fname
    fhandle = open(x[0].replace("all.cdb", "rar"), 'wb')
    fname=x[0].replace("all.cdb", "rar")
    Getting_SPO.fhandle=fhandle
    print( ' \n Getting ' + Spo_file )#for confort sake, shows the file that's being retrieved
    ftp.retrbinary('RETR ' + Spo_file, fhandle.write)
    fhandle.close()
    return Spo_file