2

I'm having a strange problem with Nordic characters 'æøå' when I download using ftplib.

When using this part of the code:

source_file = 'file_w_æøå.zip'
ftp.retrbinary('RETR %s' % source_file, open(local_file, 'wb').write)

I get this error:

return getattr(self._sock,name)(*args)
UnicodeEncodeError: 'ascii' codec can't encode characters u'\xe6' in position 12-14: ordinal not in range(128)

The funny thing is, if i replace the string with the real value like this:

ftp.retrbinary('RETR %s' % 'file_w_æøå.zip', open(local_file, 'wb').write)

The file is downloaded with no problems?!

I've tried almost everything with decode/encode with utf-8, I'm only missing to try the right thing :)

Hopefully you guys can help :)

PS When in use special characters in the local_file variable, the it works with out errors.


Full code added, not picture perfect, still a newbi:

#!/usr/local/bin/python
# -*- coding: utf-8 -*-
import os
import sys 
import ConfigParser 
import ftplib
import codecs

sysCodec = sys.getfilesystemencoding()
print('sysCodec: %s' % sysCodec)

# Get the argument (a) Section in config file
extract_conf = sys.argv[1]

configParser = ConfigParser.SafeConfigParser()
configFilePath = r'C:\\FTP_DL\\config.ini'
configParser.read(configFilePath)

#set variable from Config file
try:
    host = configParser.get(extract_conf, 'url')
    #print('Host %s'  % host)
    user = configParser.get(extract_conf, 'user') 
    #print('User %s'  % user)
    pw = configParser.get(extract_conf, 'pw')
    #print('PW %s'  % pw)
    ftp_dir = configParser.get(extract_conf, 'ftp_dir').decode('utf-8')
    print('FTP_DIR: %s'  % ftp_dir)
    dest_dir = configParser.get(extract_conf, 'dest_dir').decode('utf-8')
    print('DEST_DIR: %s'  % dest_dir)
    dest_file = configParser.get(extract_conf, 'dest_file').decode('utf-8')
    print('DEST_FILE: %s'  % dest_file)
    source_file = configParser.get(extract_conf, 'source_file').decode('utf-8')
    print('SOURCE_FILE: %s'  % source_file)
    local_file = os.path.join(dest_dir, dest_file)
    print('Local_File: %s' % local_file)
except 'NoSectionError':
    print ('Section %s not found' % extract_conf)
    sys.exit()


ftp = ftplib.FTP(host)
ftp.login(user,pw)
ftp.cwd(ftp_dir)
ftp.retrbinary(retr, open(local_file, 'wb').write) 
ftp.quit()  
  • are you on python 2.x? Encoding is a nightmare. Double check, that the encoding of your script matches that of your shell to save you a lot of trouble. Use unicode if possible `u'your äöü string'` – Dschoni Sep 15 '17 at 11:43
  • With Python 2, the classic pitfalls are: missing `# coding: utf-8` at the top of Python scripts, missing `u""` prefix in front of strings. – Laurent LAPORTE Sep 15 '17 at 11:50
  • @Dschoni, I'm using python 2.7. It sounds like encoding is not easy, i'm glad to hear that. – Sebastian Buus Jensen Sep 15 '17 at 11:55
  • @LaurentLAPORTE, I'm having `#!/usr/local/bin/python # -*- coding: utf-8 -*-` in the first two lines of my code. – Sebastian Buus Jensen Sep 15 '17 at 11:57
  • @SebastianBuusJensen are you by any chance developing with spyder? I convert all my input to unicode as a first step, to have one unified encoding inside my script. IDEs sometimes use different encoding in the editor and in the shell, which makes debugging a lot more difficult. – Dschoni Sep 15 '17 at 12:04
  • Can you try to pass the string as unicode to `ftp.retrbinary()`? – Dschoni Sep 15 '17 at 12:06
  • @Dschoni, I'm using Notepad++ I don't know spyder. I've tried to change my files to UTF-8 w/o BOM and back to ANSI and stuff like that. If that's what your thinking about? – Sebastian Buus Jensen Sep 15 '17 at 12:13
  • @Dschoni, sorry, still a newbi on coding, what do you mean by pass the strin to unicode? Is't that what I'm doing in my example, where it works? `ftp.retrbinary('RETR %s' % 'file_w_æøå.zip', open(local_file, 'wb').write)` – Sebastian Buus Jensen Sep 15 '17 at 12:16
  • Oh and what is `ftp`? – Dschoni Sep 15 '17 at 12:30
  • Can you post the whole code? – Dschoni Sep 15 '17 at 12:34
  • @Dschoni, just added the code to the post. – Sebastian Buus Jensen Sep 15 '17 at 13:51

0 Answers0