Skip to main content
added 6 characters in body
Source Link
s6hebern
  • 1.2k
  • 11
  • 21

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcodereturncode != 0: print stdout print stderr 

To be a bit more specific regarding your actual problem (using gdal_translate in a loop on many files), you could do it like this:

import os import subprocess as sub # get your files path = r'c:\MY_PATH' files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jp2')] # now run gdal_translate on each file for f in files: basename = os.path.splitext(os.path.basename(f))[0] newname = os.path.join(path, basename + '_translated.tif') # set up call my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdal_translate', '-of', 'GTiff', '-co', 'TFW=YES', f, newname] # call it print 'Using gdal_translate on {0} to convert it to {1}'.format(f, newname) p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcodereturncode != 0: print stdout print stderr 

Instead of using this way of getting your filenames you could also just create a fixed list of files as you did initially. subprocess (or sub in my case) will open the OSGEO4W Shell in the background and perform the given my_call.

I assign this to the variable p to be able to retrieve its exitreturn code (0 if it executed successfully) to see if something went wrong and print not only the error message (stderr), but also the "normal" messages produced by gdal_translate (stdout).

You can test the script by leaving out the actual call (everything below # call it) and use print my_call instead just to see what the actual would look like.

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

To be a bit more specific regarding your actual problem (using gdal_translate in a loop on many files), you could do it like this:

import os import subprocess as sub # get your files path = r'c:\MY_PATH' files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jp2')] # now run gdal_translate on each file for f in files: basename = os.path.splitext(os.path.basename(f))[0] newname = os.path.join(path, basename + '_translated.tif') # set up call my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdal_translate', '-of', 'GTiff', '-co', 'TFW=YES', f, newname] # call it print 'Using gdal_translate on {0} to convert it to {1}'.format(f, newname) p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

Instead of using this way of getting your filenames you could also just create a fixed list of files as you did initially. subprocess (or sub in my case) will open the OSGEO4W Shell in the background and perform the given my_call.

I assign this to the variable p to be able to retrieve its exit code (0 if it executed successfully) to see if something went wrong and print not only the error message (stderr), but also the "normal" messages produced by gdal_translate (stdout).

You can test the script by leaving out the actual call (everything below # call it) and use print my_call instead just to see what the actual would look like.

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: print stdout print stderr 

To be a bit more specific regarding your actual problem (using gdal_translate in a loop on many files), you could do it like this:

import os import subprocess as sub # get your files path = r'c:\MY_PATH' files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jp2')] # now run gdal_translate on each file for f in files: basename = os.path.splitext(os.path.basename(f))[0] newname = os.path.join(path, basename + '_translated.tif') # set up call my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdal_translate', '-of', 'GTiff', '-co', 'TFW=YES', f, newname] # call it print 'Using gdal_translate on {0} to convert it to {1}'.format(f, newname) p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.returncode != 0: print stdout print stderr 

Instead of using this way of getting your filenames you could also just create a fixed list of files as you did initially. subprocess (or sub in my case) will open the OSGEO4W Shell in the background and perform the given my_call.

I assign this to the variable p to be able to retrieve its return code (0 if it executed successfully) to see if something went wrong and print not only the error message (stderr), but also the "normal" messages produced by gdal_translate (stdout).

You can test the script by leaving out the actual call (everything below # call it) and use print my_call instead just to see what the actual would look like.

added 1625 characters in body
Source Link
s6hebern
  • 1.2k
  • 11
  • 21

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

To be a bit more specific regarding your actual problem (using gdal_translate in a loop on many files), you could do it like this:

import os import subprocess as sub # get your files path = r'c:\MY_PATH' files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jp2')] # now run gdal_translate on each file for f in files: basename = os.path.splitext(os.path.basename(f))[0] newname = os.path.join(path, basename + '_translated.tif') # set up call my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdal_translate', '-of', 'GTiff', '-co', 'TFW=YES', f, newname] # call it print 'Using gdal_translate on {0} to convert it to {1}'.format(f, newname) p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

Instead of using this way of getting your filenames you could also just create a fixed list of files as you did initially. subprocess (or sub in my case) will open the OSGEO4W Shell in the background and perform the given my_call.

I assign this to the variable p to be able to retrieve its exit code (0 if it executed successfully) to see if something went wrong and print not only the error message (stderr), but also the "normal" messages produced by gdal_translate (stdout).

You can test the script by leaving out the actual call (everything below # call it) and use print my_call instead just to see what the actual would look like.

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

To be a bit more specific regarding your actual problem (using gdal_translate in a loop on many files), you could do it like this:

import os import subprocess as sub # get your files path = r'c:\MY_PATH' files = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.jp2')] # now run gdal_translate on each file for f in files: basename = os.path.splitext(os.path.basename(f))[0] newname = os.path.join(path, basename + '_translated.tif') # set up call my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdal_translate', '-of', 'GTiff', '-co', 'TFW=YES', f, newname] # call it print 'Using gdal_translate on {0} to convert it to {1}'.format(f, newname) p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

Instead of using this way of getting your filenames you could also just create a fixed list of files as you did initially. subprocess (or sub in my case) will open the OSGEO4W Shell in the background and perform the given my_call.

I assign this to the variable p to be able to retrieve its exit code (0 if it executed successfully) to see if something went wrong and print not only the error message (stderr), but also the "normal" messages produced by gdal_translate (stdout).

You can test the script by leaving out the actual call (everything below # call it) and use print my_call instead just to see what the actual would look like.

added 4 characters in body
Source Link
s6hebern
  • 1.2k
  • 11
  • 21

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(cmdmy_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(cmd, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 

Adding to what @bennos said, I needed to run some commands in Windows PowerShell and ran into a similar problem. You can easily switch to OSGeo4W-Shell by inserting its program call to your actual call, like this:

import subprocess as sub # adjust path if necessary my_call = [r'C:\Program Files\QGIS 2.18\OSGeo4W.bat', 'gdalbuildvrt', '-input_file_list', vrt_textfile, vrt_file] # call it p = sub.Popen(my_call, stdout=sub.PIPE, stderr=sub.PIPE) stdout, stderr = p.communicate() if p.exitcode != 0: print stdout print stderr 
Source Link
s6hebern
  • 1.2k
  • 11
  • 21
Loading