subprocess.call with non-ASCII arguments?

Laura Creighton lac at openend.se
Tue Dec 15 10:08:23 EST 2015


In a message of Tue, 15 Dec 2015 14:25:50 +0000, Ulli Horlacher writes:
>(My first posting seems to got lost)
>
>I want to create a zip file within a Python 2.7 program on windows.
>
>My code:
>
>  cmd = ['7za.exe','a','-tzip',archive] + files
>  status = subprocess.call(cmd)
>
>leads to:
>
>  File "fexit.py", line 971, in sendfile_retry
>    status = subprocess.call(cmd)
>  File "C:\Python27\lib\subprocess.py", line 522, in call
>    return Popen(*popenargs, **kwargs).wait()
>  File "C:\Python27\lib\subprocess.py", line 710, in __init__
>    errread, errwrite)
>  File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
>    startupinfo)
>UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 87:
> ordinal not in range(128)
>
>
>This is because the array "files" contains filenames with non-ASCII
>characters.
>
>So, the problem is in subprocess.py, which I cannot modify.
>
>
>Instead of calling a 7z subprocess with non-ASCII arguments I tried to
>call it with a listfile: it starts with a "@" and contains the names of
>the files to be packed into the arcive. It is a special 7z feature.
>
>New code:
>
>  fileslist = archive + '.list'
>  flo = open(fileslist,'w')
>  for file in files: print(file,file=flo)
>  flo.close()
>  cmd = ['7za.exe','a','-tzip',archive,'@'+fileslist]
>  status = subprocess.call(cmd)
>
>
>But with that I get a new error:
>
>  File "fexit.py", line 959, in sendfile_retry
>    for file in files: print(file,file=flo)
>UnicodeEncodeError: 'ascii' codec can't encode character u'\xf6' in position 8:
>  ordinal not in range(128)
>
>
>I get the same error message, when i use:
>      flo = open(fileslist,'wb')
>      
>
>How can I tell open() or print() that I want to write non-ASCII ?

see if setting the environment variable PYTHONIOENCODING
https://docs.python.org/2/using/cmdline.html

works for you.  No promises, not a windows user.

Laura



More information about the Python-list mailing list