Delete hidden files on unix

Discussion in 'Python' started by loial, Mar 3, 2008.

  1. loial

    loial Guest

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

    rm .lock*
     
    loial, Mar 3, 2008
    #1
    1. Advertisements

  2. loial

    Peter Otten Guest

    loial wrote:

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


    >>> for fn in glob.glob(".lock*"):

    .... os.remove(fn)
    ....

    Peter
     
    Peter Otten, Mar 3, 2008
    #2
    1. Advertisements

  3. loial <> wrote:
    > 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

    --
    Dr. Philipp Pagel
    Lehrstuhl f. Genomorientierte Bioinformatik
    Technische Universität München
    http://mips.gsf.de/staff/pagel
     
    Philipp Pagel, Mar 3, 2008
    #3
  4. loial

    subeen Guest

    On Mar 3, 6:13 pm, Philipp Pagel <>
    wrote:
    > loial <> wrote:
    > > 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
    >
    > --
    > Dr. Philipp Pagel
    > Lehrstuhl f. Genomorientierte Bioinformatik
    > Technische Universität Münchenhttp://mips.gsf.de/staff/pagel


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

    regards,
    subeen.
    http://love-python.blogspot.com
     
    subeen, Mar 3, 2008
    #4
  5. loial

    Guest

    On Mar 3, 9:38 am, subeen <> wrote:
    > On Mar 3, 6:13 pm, Philipp Pagel <>
    > wrote:
    >
    >
    >
    > > loial <> wrote:
    > > > 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

    >
    > > --
    > > Dr. Philipp Pagel
    > > Lehrstuhl f. Genomorientierte Bioinformatik
    > > Technische Universität Münchenhttp://mips.gsf.de/staff/pagel

    >
    > 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.
     
    , Mar 3, 2008
    #5
  6. loial

    Tim Roberts Guest

    loial <> wrote:
    >
    >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?
    --
    Tim Roberts,
    Providenza & Boekelheide, Inc.
     
    Tim Roberts, Mar 5, 2008
    #6
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Javier
    Replies:
    2
    Views:
    1,033
    Markus Schoder
    Oct 5, 2007
  2. Rick
    Replies:
    3
    Views:
    8,895
    Alexey Smirnov
    Apr 13, 2010
  3. Jason  Priest
    Replies:
    2
    Views:
    196
    Joe Smith
    Jan 8, 2005
  4. Ignoramus6539
    Replies:
    2
    Views:
    306
    Colin McKinnon
    Aug 30, 2006
  5. SolarCanine
    Replies:
    2
    Views:
    380
    SolarCanine
    Sep 20, 2005
Loading...

Share This Page