Delete hidden files on unix

L

loial

How can I delete hidden files on unix with python, i.e I want to do
equivalent of

rm .lock*
 
P

Philipp Pagel

loial said:
How can I delete hidden files on unix with python, i.e I want to do
equivalent of
rm .lock*

Here is one way to do it:

import os, glob
for filename in glob.glob('.lock*'):
os.unlink(filename)


Alternatively, you could also do this:

import os
os.system('rm .lock*')


cu
Philipp
 
S

sjdevnull

Another way is to execute the linux command directly :)
Check here:http://love-python.blogspot.com/2008/02/execute-linux-commands-in-pyt...

Note that that can get dangerous with shell expansions:

e.g. (don't run in a directory with files you want to keep!)
import os
open("--help", "w")
os.system("rm *help")

will actually run "rm --help", printing the help for rm rather than
removing the file named "--help". There are a lot of security
implications to allowing this kind of shell expansion of commands.
The system-call method with os.unlink is easier to get right.
 
T

Tim Roberts

loial said:
How can I delete hidden files on unix with python, i.e I want to do
equivalent of

rm .lock*

What did you try? I'm curious to know what you tried that didn't work,
because I can't think of any obvious solution to this that would not just
work.

You did try to solve this yourself before sending a message around the
world, didn't you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top