Python Forum
<SOLVED>os.system needs a string in quotes
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
<SOLVED>os.system needs a string in quotes
#1
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
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#2
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]
Recommended Tutorials:
Reply
#3
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 `('
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#4
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
You should also use Subprocess it replace use of older os.system().
Reply
#6
(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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
(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
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#8
(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 ]
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(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'
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply
#10
You shall call it without [] when argument are in a list.
 
subprocess.call(cmd)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help on how to include single quotes on data of variable string hani_hms 5 2,165 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 1,572 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 1,149 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 1,317 Jul-11-2022, 09:22 AM
Last Post: ibreeden
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,286 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,261 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Replace String with increasing numer [SOLVED] AlphaInc 13 5,204 Aug-07-2021, 08:16 AM
Last Post: perfringo
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,325 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  reading lines from a string [Solved] ebolisa 14 6,513 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Two types of single quotes Led_Zeppelin 2 1,960 Mar-15-2021, 07:55 PM
Last Post: BashBedlam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020