0

I recently installed Ubuntu to run alongside my Windows OS. I wanted to see how a certain script ran in Ubuntu and it ran fine for the most part. There is this one part of my code that causes trouble. I try to open a file using the os.system('gnome-open ' + filePath) command but I can't get it to open a file unless I only specify the file name not the directory (i.e. I have to say "data.txt", I can't say "home/user/workspace/project/src/data.txt" because it'll say the file/directory doesn't exist). Also I made multiple copies of this file for testing purposes and some of them have parentheses in their names, when I attempt to open these files I get the error "sh: Syntax error: "(" unexpected" and it doesn't specify a line of code so I assume it's the line that's accessed when I call this function. Below is the code I'm referencing.

def openFileOfItem(self, row):
        print fileList[row]
        if platform.system() == "Windows":
            os.startfile(fileList[row])
        else:
            if platform.system() == "Linux":
                os.system('gnome-open ' + nameList[row])
            else:
                os.system('open %s' % fileList[row])

And some sample output:

/home/damian/workspace/Kde Gen/src/data.txt
Error showing url: Error stating file '/home/damian/workspace/Kde': No such file or directory
/home/damian/workspace/Kde Gen/src/data (copy).txt
sh: Syntax error: "(" unexpected
DamianJ
  • 419
  • 2
  • 8
  • 18
  • 1
    Check this out. You may just need to escape the spaces: http://stackoverflow.com/questions/35817/how-to-escape-os-system-calls-in-python – jamesmortensen Mar 11 '12 at 03:59

4 Answers4

3

You are dumping the string directly to the command line without escaping - this results in errors when the shell tries to execute the command you provided to it. You need to escape your file path first. Since you are using Python 2.7, try using pipes.quote

from pipes import quote

def openFileOfItem(self, row):
    print fileList[row]
    if platform.system() == "Windows":
        os.startfile(fileList[row])
    else:
        if platform.system() == "Linux":
            os.system('gnome-open %s' % quote(nameList[row]))
        else:
            os.system('open %s' % quote(fileList[row]))
Community
  • 1
  • 1
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • 1
    Last time I checked, pipes.quote was not perfect, but it's better than just throwing in double quotes (as in another answer here). It will do until Python comes with a proper quoter. :-) – torek Mar 11 '12 at 04:21
2

You are effectively running a shell command, and it's getting tripped up by the spaces in your path. Instead of quoting the arguments, it's cleaner to do this:

import subprocess
subprocess.call([ "gnome-open", nameList[row] ] )
alexis
  • 48,685
  • 16
  • 101
  • 161
  • I think you forgot to put those arguments in a list, at least I had to to get it to work. Thanks anyways, I looked at the docs for subprocess and it seems to be the intended replacement for the other options mentioned. – DamianJ Mar 11 '12 at 20:58
  • Oops! Indeed I did. Fixed now. – alexis Mar 11 '12 at 21:35
0

AFAICT, the error message is because you have space in the path. quote the path should fix the problem.

os.system('gnome-open "%s"' % nameList[row])
Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
0

I can't say "home/user/workspace/project/src/data.txt" because it'll say the file/directory doesn't exist)

Did you mean "/home/user/workspace/project/src/data.txt" ? Otherwise, it's relative to the current directory.

Other than that, on UNIX it's customary to use the version that runs the program directly and passes an argument vector, otherwise the shell (used by system()) might do weird stuff which you told it without intending to (that is called injection).

os.spawnvp(os.P_WAIT, "gnome-open", ["gnome-open", nameList[row]])