# gdal_translate called via subprocess.check_output works when called directly, but not from within arcgis

EDIT: Solved it myself. Beginners error... :/

I am writing a python Toolbox that should use gdal_translate to convert an ASCII Gridded XYZ-File into a lower resolution (and doing afterwards a couple of other things).

For that I have written the following snippet:

filepath=r'X:\some_path\to\file\with_only_ascii_and_no_whitespaces\file.xyz'
try:
inf=filepath
outf=filepath + '__dgm20.xyz'
result = subprocess.check_output(
[
'gdal_translate',
'-of',
'XYZ',
'-tr',
'20',
'20',
inf.encode(locale.getpreferredencoding()),
outf.encode(locale.getpreferredencoding())
],
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as err:
else:


When I run this from the python console in cmd.exe or the ArcGIS python console, everything is fine, my file will be created. But when I run it from within a python toolbox (after retrieving filepath from a parameter and normalizing it with os.path) I get this error:

ERROR 1: At line 3999971, found 1 tokens. Expected 3 at least

The processed file (filepath) ist totally fine, I think.

So, why is this happening? What is it I can't figure out?

I solved my problem myself. But sadly it wasn't caused by the code snippet I provided.

The indentation was the problem. The csv hasn't been fully written down, when i tried to start working with it in gdal_translate.

filepath=r'X:\some_path\to\file\with_only_ascii_and_no_whitespaces\file.xyz'
with open(filepath, 'r') as xyzfile:
with open(filepath + '.txt', 'wb') as csvfile:
xyzfile.seek(0)
df = df.dropna(axis=1, how='all')
f = df.columns.values.tolist()
df = df.sort_values(by=[f[1], f[0]])

try: #here starts the above snippet
inf=filepath + '.txt'
outf=filepath + '__dgm20.xyz'
[...]


correct is:

filepath=r'X:\some_path\to\file\with_only_ascii_and_no_whitespaces\file.xyz'
with open(filepath, 'r') as xyzfile:
with open(filepath + '.txt', 'wb') as csvfile:
[...]
try:
inf=filepath + '.txt'
outf=filepath + '__dgm20.xyz'
result = subprocess.[...]


Sorry for any caused loss of time.