AWS Thinkbox Discussion Forums

python 2.7 vs unicode

We are trying to pass a file name that contains the “½” character to a program that trims the image, but it keeps failing no matter how we do it … so any suggestions as to what we are doing wrong would be appriciated :slight_smile:

we are using deadline 8.0.13.3 and python 2.7.11
(we will be upgrading to 10 soon if that makes any difference)

Could you post some error text as well?

I wonder if it’s because you’re shelling out with that character and the shell or Windows API doesn’t like it. Doing some Googling, this makes sense:
stackoverflow.com/a/9951851/187769

So, instead of using UTF-8, use what the shell is expecting. From that link:

>>> import subprocess
>>> import locale
>>> subprocess.Popen(u'Pokémon½.mp3'.encode(locale.getpreferredencoding()),shell=True)

thanks for taking a look :slight_smile:
we tried changing the script to this:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import subprocess
import time
import urllib
import urllib2
import os
import sys
import json
import locale

from System.IO import *

from Deadline.Scripting import *

def __main__( *args ):
	deadlinePlugin = args[0]
	job = deadlinePlugin.GetJob()
	outputDirectories = job.OutputDirectories
	outputFilenames = job.OutputFileNames
	jobid = job.JobId


	fullfilename = u'{0}\{1}'.format(outputDirectories[0], outputFilenames[0])

	time.sleep(3)

	exeFile = u'"l:\Clients\#ImageCustomizer\Tools\PNGTrim\PNGtrim.exe" trim=tblr i="{0}"'.format(fullfilename)

	startupinfo = subprocess.STARTUPINFO()
	startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
	startupinfo.wShowWindow = subprocess.SW_HIDE

	jobaction = 'delete'


	if exeFile.find(r'½') < 0:
		p = subprocess.Popen(exeFile.encode(locale.getpreferredencoding()), startupinfo=startupinfo)
		stdout, stderr = p.communicate()

		if os.path.isfile(fullfilename):
			statinfo = os.stat(fullfilename)
			if statinfo.st_size < 2048:
				jobaction = 'fail'
				print ("File is too small - less than 2KB")
		else:
			print ("File does not exist")
			jobaction = 'fail'




	if jobaction == 'delete':
		opener = urllib2.build_opener(urllib2.HTTPHandler)
		request = urllib2.Request("http://cs02:8082/api/jobs?JobID=" + jobid)
		request.add_header('Content-Type', 'application/json')
		request.get_method = lambda: 'DELETE'
		url = opener.open(request)
	else:
		opener = urllib2.build_opener(urllib2.HTTPHandler)
		request = urllib2.Request("http://cs02:8082/api/jobs", data = json.dumps({'Command': 'fail', 'JobID': jobid}))
		request.add_header('Content-Type', 'application/json')
		request.get_method = lambda: 'PUT'
		url = opener.open(request)

and got this error:

we got it working … still not sure how! :slight_smile:

Oh great! I fought with it a little yesterday, but I had no luck making it through. If you do figure it out, feel free to post here.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import re
import subprocess
import time
import urllib
import urllib2
import os
import sys
import json
import locale

from System.IO import *

from Deadline.Scripting import *

def __main__( *args ):
	deadlinePlugin = args[0]
	job = deadlinePlugin.GetJob()
	outputDirectories = job.OutputDirectories
	outputFilenames = job.OutputFileNames
	jobid = job.JobId


	fullfilename = u'{0}\{1}'.format(outputDirectories[0], outputFilenames[0])

	time.sleep(3)

	exeFile = u'"l:\Clients\#ImageCustomizer\Tools\PNGTrim\PNGtrim.exe" trim=tblr i="{0}"'.format(fullfilename)

	startupinfo = subprocess.STARTUPINFO()
	startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
	startupinfo.wShowWindow = subprocess.SW_HIDE

	jobaction = 'delete'



	p = subprocess.Popen(exeFile.encode(locale.getpreferredencoding()), startupinfo=startupinfo)
	stdout, stderr = p.communicate()

	if os.path.isfile(fullfilename):
		statinfo = os.stat(fullfilename)
		if statinfo.st_size < 2048:
			jobaction = 'fail'
			print ("File is too small - less than 2KB")
	else:
		print ("File does not exist")
		jobaction = 'fail'




	if jobaction == 'delete':
		opener = urllib2.build_opener(urllib2.HTTPHandler)
		request = urllib2.Request("http://cs02:8082/api/jobs?JobID=" + jobid)
		request.add_header('Content-Type', 'application/json')
		request.get_method = lambda: 'DELETE'
		url = opener.open(request)
	else:
		opener = urllib2.build_opener(urllib2.HTTPHandler)
		request = urllib2.Request("http://cs02:8082/api/jobs", data = json.dumps({'Command': 'fail', 'JobID': jobid}))
		request.add_header('Content-Type', 'application/json')
		request.get_method = lambda: 'PUT'
		url = opener.open(request)

pretty sure we added an extra “u” somwhere … to make it work … :slight_smile:
the problem is that I can’t code, and the guy helping me write the script don’t know python, so we are doing a lot of copy n pasting and wishing for the best!

That’s how I got started. :smiley:

Except my Commodore 64 meant that “pasting” from a book took a little longer than it does today.

Privacy | Site terms | Cookie preferences