Python Forum
<SOLVED>os.system needs a string in quotes - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: <SOLVED>os.system needs a string in quotes (/thread-292.html)

Pages: 1 2


<SOLVED>os.system needs a string in quotes - Fred Barclay - Oct-04-2016

G'day! I've written an extension for Caja (the default file manager in MATE) that wipes files/folders using the srm command. The source code is here: https://github.com/Fred-Barclay/Caja-Wipe in src/caja-wipe.py.
(Note: if you don't have Caja but are familiar with Nemo/Nautilus: the python actions are identical, just with "caja" in place of "nemo" or "nautilus").

Here is the relevant code I'm having trouble with:
# Aaaannnnnndddddd....... ACTION!
    def wipe_file(self, menu, file):
        for filename in filelist:
            path = pwd+"/"+filename
            path = str(path)
            if not os.access(path, os.W_OK):
                print("You do not have permission to wipe this file") # Debugging
                return
            # print(filelist) # Debugging
            # print(filename) # Debugging
            print(path) # Debugging
            cmd = "srm -rv "+path # -v is good for debugging
            os.system(cmd)
If I try to delete a file/folder with a special character in its name, such as whitespace or a parentheses, the shell called by os.system throws out an error. Say I'm deleting a folder with a parentheses. Here's what I'll see:
Error:
/home/fred/Git/test (folder) sh: 1: Syntax error: "(" unexpected
Now of course, if I were running srm -rv /home/fred/Git/test (folder) in the shell itself, rather than in python, I would surround it with quotes: srm -rf "/home/fred/Git/test (folder)", and I'd be able to wipe the folder without any issue. But since I'm calling this command from within Python, I can't figure out how to surround it with quotes.

I've tried mostly variations on
path = str(path)
on line 5, but I haven't had any luck with that. I've also tried such oddities as
path = "%s" % path
but to no avail. :wall:

One thing I have noticed (tipped off by "path = "%s" % path") is that path seems to already be a string, albeit without quotes. Not sure exactly how that happens, but I have a feeling that is to blame.

So...
how can I get path inside quotes so that I can successfully delete files and folders with special characters?

Thanks!
Fred

Some additional info I should have included in the original post (oops!):
OS: LMDE 2 "Betsy" MATE 64-bit
Python version: 3.4.2


RE: os.system needs a string in quotes - metulburr - Oct-04-2016

Quote:Now of course, if I were running srm -rv /home/fred/Git/test (folder) in the shell itself, rather than in python, I would surround it with quotes: srm -rf "/home/fred/Git/test (folder)"
You should be able to escape the space via

Output:
[color=#333333]srm -rv /home/fred/Git/test\ (folder)[/color]



RE: os.system needs a string in quotes - Fred Barclay - Oct-04-2016

Hi metulburr. This is what I get when I run that (in shell, not Python):
Output:
$ srm -rv /home/fred/Git/test\ (folder) bash: syntax error near unexpected token `('



RE: os.system needs a string in quotes - wavic - Oct-04-2016

bash escapes this characters when you hit tab on auto-complete:

 !"$&'()*,:;<=>?@[\]^`{|} # plus lisht space
Here is what you get with echo:
Output:
$ echo ( bash: syntax error near unexpected token `newline'
And with escaping the '(':
Output:
$ echo \( (

Why you don't use os.path.join() instead of  pwd+"/"+filename


RE: os.system needs a string in quotes - snippsat - Oct-04-2016

You should also use Subprocess it replace use of older os.system().


RE: os.system needs a string in quotes - Ofnuts - Oct-05-2016

(Oct-04-2016, 09:02 PM)snippsat Wrote: You should also use Subprocess it replace use of older os.system().

... and use it to call you srm command directly (ie, without a intermediate shell). Then each of your command parameters is a string in a list and you completely avoid the shell syntax issues.


RE: os.system needs a string in quotes - Fred Barclay - Oct-06-2016

(Oct-05-2016, 08:42 AM)Ofnuts Wrote:
(Oct-04-2016, 09:02 PM)snippsat Wrote: You should also use Subprocess it replace use of older os.system().

... and use it to call you srm command directly (ie, without a intermediate shell). Then each of your command parameters is a string in a list and you completely avoid the shell syntax issues.

Hmm... that would be nice but this is what I get when srm-ing /home/fred/Git/test (folder):
Error:
/home/fred/Git/test (folder) Traceback (most recent call last):   File "/home/fred/.local/share/caja-python/extensions/caja-wipe.py", line 112, in wipe_file     subprocess.call([cmd])   File "/usr/lib/python2.7/subprocess.py", line 522, in call     return Popen(*popenargs, **kwargs).wait()   File "/usr/lib/python2.7/subprocess.py", line 710, in __init__     errread, errwrite)   File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child     raise child_exception OSError: [Errno 2] No such file or directory
Applicable code is this:
    def wipe_file(self, menu, file):
        for filename in filelist:
            path = pwd+"/"+filename
            path = str(path)
            if not os.access(path, os.W_OK):
                print("You do not have permission to wipe this file") # Debugging
                return
            # print(filelist) # Debugging
            # print(filename) # Debugging
            print(path) # Debugging
            cmd = "srm -rv "+path # -v is good for debugging
            subprocess.call([cmd])
If needed, the entire source code is on GitHub:
https://github.com/Fred-Barclay/Caja-Wipe
(Look in src/caja-wipe.py)

Thanks for all help so far!

EDIT: Python 3.4


RE: os.system needs a string in quotes - Skaperen - Oct-06-2016

(Oct-06-2016, 12:03 AM)Fred Barclay Wrote:
(Oct-05-2016, 08:42 AM)Ofnuts Wrote:
(Oct-04-2016, 09:02 PM)snippsat Wrote: You should also use Subprocess it replace use of older os.system().

... and use it to call you srm command directly (ie, without a intermediate shell). Then each of your command parameters is a string in a list and you completely avoid the shell syntax issues.

Hmm... that would be nice but this is what I get when srm-ing /home/fred/Git/test (folder):
Error:
/home/fred/Git/test (folder) Traceback (most recent call last):   File "/home/fred/.local/share/caja-python/extensions/caja-wipe.py", line 112, in wipe_file     subprocess.call([cmd])   File "/usr/lib/python2.7/subprocess.py", line 522, in call     return Popen(*popenargs, **kwargs).wait()   File "/usr/lib/python2.7/subprocess.py", line 710, in __init__     errread, errwrite)   File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child     raise child_exception OSError: [Errno 2] No such file or directory
Applicable code is this:
    def wipe_file(self, menu, file):
        for filename in filelist:
            path = pwd+"/"+filename
            path = str(path)
            if not os.access(path, os.W_OK):
                print("You do not have permission to wipe this file") # Debugging
                return
            # print(filelist) # Debugging
            # print(filename) # Debugging
            print(path) # Debugging
            cmd = "srm -rv "+path # -v is good for debugging
            subprocess.call([cmd])
If needed, the entire source code is on GitHub:
https://github.com/Fred-Barclay/Caja-Wipe
(Look in src/caja-wipe.py)

Thanks for all help so far!

EDIT: Python 3.4

try:

cmd = [ "srm", "-rv", path ]



RE: os.system needs a string in quotes - Fred Barclay - Oct-06-2016

(Oct-06-2016, 12:55 AM)Skaperen Wrote: try:

cmd = [ "srm", "-rv", path ]

I get
Error:
/home/fred/Git/test (folder) Traceback (most recent call last):   File "/home/fred/.local/share/caja-python/extensions/caja-wipe.py", line 113, in wipe_file     subprocess.call([cmd])   File "/usr/lib/python2.7/subprocess.py", line 522, in call     return Popen(*popenargs, **kwargs).wait()   File "/usr/lib/python2.7/subprocess.py", line 710, in __init__     errread, errwrite)   File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child     raise child_exception AttributeError: 'list' object has no attribute 'rfind'



RE: os.system needs a string in quotes - snippsat - Oct-06-2016

You shall call it without [] when argument are in a list.
 
subprocess.call(cmd)