subprocess.call with non-ASCII arguments?

Ulli Horlacher framstag at rus.uni-stuttgart.de
Tue Dec 15 10:05:00 EST 2015


Ulli Horlacher <framstag at rus.uni-stuttgart.de> wrote:

> 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 found a partial solution:

    for file in files: print(file.encode('utf8'),file=flo)

But this works only for files I get from Tk askopenfilename(), not for
files from sys.argv[]
Then I see:

S:\>python fexit.py -a x.zip C:\Users\admin\_XöX.exe .
files selected:

"C:\Users\admin\_X÷X.exe"
2015-12-07 16:17:15
114 kB

Traceback (most recent call last):
  File "fexit.py", line 2166, in <module>
    wexit(main())
  File "fexit.py", line 260, in main
    status = sendfile_retry(files,recipient,comment)
  File "fexit.py", line 959, in sendfile_retry
    for file in files: print(file.encode('utf8'),file=flo)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 17:
 ordinal not in range(128)


-- 
Ullrich Horlacher              Server und Virtualisierung
Rechenzentrum IZUS/TIK         E-Mail: horlacher at tik.uni-stuttgart.de
Universitaet Stuttgart         Tel:    ++49-711-68565868
Allmandring 30a                Fax:    ++49-711-682357
70550 Stuttgart (Germany)      WWW:    http://www.tik.uni-stuttgart.de/



More information about the Python-list mailing list