Oct-04-2016, 08:07 PM
(This post was last modified: Oct-06-2016, 05:31 PM by Fred Barclay.)
G'day! I've written an extension for Caja (the default file manager in MATE) that wipes files/folders using the
(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:
I've tried mostly variations on
One thing I have noticed (tipped off by "path = "%s" % path") is that
So...
how can I get
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
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" % pathbut 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
Editor: Atom with Material Syntax UI and the Termination terminal plugin
Micah 6:8