Search code examples
pythonsubprocesspopen

Using special characters in python Popen call


I'm working on a python 2.7 application that is intended to upload files to a 2007 Sharepoint. The server is running on a OpenSUSE machine. The way I'm doing this right now is to use subprocess' Popen to execute a curl command. However, python raises UnicodeEncodeError when the filename has special characters ('á', 'õ', 'ç', e.g.).

I could work around the Unicode Error by forcing a conversion
from str to Unicode for the command string, but then the file goes to Sharepoint with a bad formatting name (Manutenção becomes Manuten'c~ao, for instance). The following image describes the implementation of the functions I'm using: functions used

Does anyone have a clue on how should I proceed?

P.S.: I would prefer to stay with the curl command for the execution of the request, but we're open to other solutions

EDIT: Didn't try pycurl, but I'll take a look on it. The Linux dist running the server is 3.11.10. The execution flow is as following:

  1. Read the file name and path from a utf-8 database
  2. Choose the destination url for file in Sharepoint
  3. Build a curl command similar to "curl --ntlm -u usr:pwd --upload-file filelocalPath fileSharepointUrl -k"
  4. Use the convert_to_utf_8 function (check attached image)
  5. Use Popen to make the request (as in __execute_curl_cmd_and_wait function)

Solution

  • Ok, guys, I figured out the problem

    Since I was running my code on a Linux machine, the Popen command was encoded in utf-8 and sent to Sharepoint this way. However, it seems the encoding used by Sharepoint 2007 is some of latin-1 or windows-1252.

    I came to this conclusion after running that same code on a Windows machine and encoding the line with a bunch of other encodings. As expected, the upload went OK whenever the encoding was windows-1252 or latin-1 (recognized by the win cmd).

    Thanks for the replies.