I am currently working on a QGIS plugin which depits node to node discreet flows. Let' say adaption of Tobler's Flow Mapper to QGIS.

Plugin currently tested and working under Windows. But not under Linux or MacOS X.

As far as I can figure, this is due to a path problem where user chooses the output shapefile location.

Here is what code look likes:

def OutputShp(self):
    dlg = FlowMapperDialog()
    fd = QtGui.QFileDialog(dlg)
    global SaveShpName
    global SaveShpDirectory
    SaveShpName = fd.getSaveFileName(None, 'Shapefile(*.shp)', 'Type output shapefile name', '*.shp')
...some code...

SaveDirectory = os.path.realpath(SaveShpName)
flowpyv07.shapefilemaker(FlowType,str(SaveDirectory), str(SaveShpName),str(InputMatrixName),str(InputNodesName),str(combotext))

When I run it I get the following error:

Traceback (most recent call last):

File "/home/user/.qgis//python/plugins/FlowMapper/flowmapper.py", line 1346, in run

SaveDirectory = os.path.realpath(SaveShpName)

File "/usr/lib/python2.7/posixpath.py", line 358, in realpath if isabs(filename):

File "/usr/lib/python2.7/posixpath.py", line 53, in isabs return s.startswith('/')

AttributeError: 'QString' object has no attribute 'startswith'

Python version: 2.7.3 (default, Aug 1 2012, 05:27:35) [GCC 4.6.3]

QGIS version: 1.8.0-Lisboa Lisboa, exported

However, if I enter the "SaveDirectory" path manually in the code such as "/home/user/Documents/" it works like charm. Thus I think I cannot get the path correctly. Any suggestion is valuable since users regret if it does not work under Linux.

If needed, full source code and plugin is avaliable from the QGIS repo: http://plugins.qgis.org/plugins/FlowMapper/

1 Answer 1


The Problem might be that Linux/Mac systems have a different file separator as Windows. You can print the system specific separator like this

from os import path
print path.sep 
> "/"

Therefore you have to concatenate your path elements depending on the users system. For instance like this:

import os
myhost = os.uname()[0]
if myhost == "Linux":

It might be a good idea to print out the SaveDirectory-variable on every system to see how it looks like.

Good look with your plugin ;)

  • 4
    You should really just use os.path.join and it will do it for you.
    – Nathan W
    Commented Jun 3, 2013 at 21:07
  • Thanks for valuable help. os.path.realpath(SaveShpName) returns c:\filename.shp while SaveShpName returns c:/filename.shp Just deleted os.path.realpath and now works under both Linux and Windows
    – cempro
    Commented Jun 3, 2013 at 23:29

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.