3

I am trying to write a python code whose purpose is to open windows directories corresponding to the communes I select in a "common" Shape layer (with Qgis software). I specify that the common layer contains a column X with the paths to the directories of the communes (example of the path: V: / 02_doc / commune1 /)

Here is my current code. It does not work as intended (it opens ... the windows directory "'my documents"

import os
X = os.path.dirname( unicode( qgis.utils.iface.activeLayer().dataProvider().dataSourceUri() ) )
os.system("explorer " + "[%"X"%]")
layer = qgis.utils.iface.activeLayer()
qgis.utils.iface.setActiveLayer(layer) 

This code is used to indicate a single path and point on when I click on the layer.

import subprocess
layer = iface.activeLayer()
x = layer.source()
subprocess.Popen(r'explorer /select,' + x)

That's not what I want.


`with this code:

import os
X = os.path.dirname( unicode( qgis.utils.iface.activeLayer().dataProvider().dataSourceUri() ) )
os.system("explorer "+posplu)
layer = qgis.utils.iface.activeLayer()
qgis.utils.iface.setActiveLayer(layer) 

There is a change, when I click on my layer in Qgis, I point the directory where my layer is.

That's not what I want.

My layer contains an x ​​column. The latter includes paths pointing repertory directories to cities. I would like to click on a city A and open the directory A, on a city B and open the directory B.


I found

import os layer = qgis.utils.iface.activeLayer() import subprocess subprocess.Popen(r'explorer /select r,"[% X %]"')

x is the column containing directory paths fcka

1
  • Please edit your question to include a output of printing X. In a test on my system, a typo in my path ("Downlaods" vs "Downloads") resulted in opening "My Documents".
    – SMiller
    Commented Nov 1, 2018 at 15:27

2 Answers 2

1

This line isn't valid python syntax:

os.system("explorer " + "[%"X"%]")

if I test with:

X="some path"
print("explorer " + "[%"X"%]")

I get:

>>> print("explorer " + "[%"X"%]")
  File "<stdin>", line 1
    print("explorer " + "[%"X"%]")
                            ^
SyntaxError: invalid syntax

I'm not sure what the exact syntax is to run explorer in windows, but maybe you just want to concatenate the X with the rest of the string:

>>> print("explorer " + "[%"+X+"%]")
explorer [%some path%]

If its just the path for the argument then maybe quote it:

>>> print("explorer \""+X+"\"")
explorer "some path"

Its a good practice to construct and print the string you are making before running os.system on it, for debugging purposes:

cmd = "explorer "+X
print("cmd is ",cmd)
os.system(cmd)
1
  • This is a good point Spacedman. Highlighting just one section for fcka: try changing os.system("explorer " + "[%"X"%]") to os.system(cmd) with an earlier stage building cmd = "explorer " + '"{}"'.format(X) In other words, if the line above created X = c:\users\username\Downloads\My Folder, then cmd should translate to 'explorer "c:\users\username\Downloads\My Folder"'. Currently the brackets and percentages may be making the path invalid, causing it to open Documents instead.
    – SMiller
    Commented Nov 1, 2018 at 17:04
0

I did something similar a while ago but used subprocess instead:

import subprocess
layer = iface.activeLayer()
x = layer.source()
subprocess.Popen(r'explorer /select,' + x)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.