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
1

4 Answers 4

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]))
Sign up to request clarification or add additional context in comments.

1 Comment

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. :-)
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] ] )

2 Comments

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.
Oops! Indeed I did. Fixed now.
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])

Comments

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]])

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.