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
likes this post
OS: Arch
Editor: Atom with Material Syntax UI and the Termination terminal plugin

Micah 6:8
Reply


Messages In This Thread
<SOLVED>os.system needs a string in quotes - by Fred Barclay - Oct-04-2016, 08:07 PM
RE: os.system needs a string in quotes - by wavic - Oct-04-2016, 08:45 PM
RE: os.system needs a string in quotes - by Ofnuts - Oct-05-2016, 08:42 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Open file, and insert space in string? Winfried 7 528 May-28-2025, 07:56 AM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 645 May-01-2025, 03:43 PM
Last Post: Winfried
  [SOLVED] Sub string not found in string ? jehoshua 4 1,611 Dec-03-2024, 09:17 PM
Last Post: jehoshua
Question [SOLVED] How to replace characters in a string? Winfried 2 1,223 Sep-04-2024, 01:41 PM
Last Post: Winfried
  Need help on how to include single quotes on data of variable string hani_hms 5 7,901 Jan-10-2023, 11:26 AM
Last Post: codinglearner
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into &lt;/&gt;? Winfried 0 2,828 Sep-03-2022, 11:21 PM
Last Post: Winfried
  [SOLVED] [BeautifulSoup] Turn select() into comma-separated string? Winfried 0 2,006 Aug-19-2022, 08:07 PM
Last Post: Winfried
  Adding string after every 3rd charater [SOLVED] AlphaInc 2 2,221 Jul-11-2022, 09:22 AM
Last Post: ibreeden
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 3,441 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Replace String in multiple text-files [SOLVED] AlphaInc 5 11,570 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

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