#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14

    Deleting computer files


    Hello,

    Is it possible to write a python program that goes into a Windows OS folder and deletes some particular files. I don't know how to program the deleting part.

    Along the same lines is it possible to write a Python script that executes a Windows application and cause it to open a file on the computer? My guess is that I would have to learn to work with COM objects for this, but I didn't have much luck learning them. Is there a simpler way?!

    Thanks
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Delete file:
    Code:
    import os
    os.remove(filenamehere)
    Execute file:
    Code:
    import os
    os.startfile(filenamehere)

    Comments on this post

    • monkeyman23555 agrees : Yup I think that is also the only way
    Last edited by †Yegg†; July 30th, 2005 at 01:43 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Gent, Belgium
    Posts
    19
    Rep Power
    0
    Originally Posted by sam_kh918
    Along the same lines is it possible to write a Python script that executes a Windows application and cause it to open a file on the computer? My guess is that I would have to learn to work with COM objects for this, but I didn't have much luck learning them. Is there a simpler way?!
    You can use os.system(command) for this. Command is the program + arguments (e.g. path of the file that has to be opened)
    I hope this is what you mean.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    Yes, thank you, I'm sure that does the job.
    I assume I can get a list of the functions that I can use with "os" from the Python library.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Gent, Belgium
    Posts
    19
    Rep Power
    0
    In the Python Menu in your Start Menu, there is Module Docs, there you can find all modules and their documention, methods...
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    well the problem is he was not sure because i don't think you want to go through a few hundred modules before you find what you need
    Those people who think they know everything are a great annoyance to those of us who do.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Gent, Belgium
    Posts
    19
    Rep Power
    0
    True, but you can always use the search function of pydoc, so it isn't that hard to browse through those modules.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    330
    Rep Power
    14
    Ok I know I'm pushing it now! but now that I am able to open a program on my Windows computer, is there any way to make that program open a file in it?
    For example I have been able to write a python script that opens a Windows program called LaserGene, now can I also tell my script to open a file from my computer in this LaserGene?
  16. #9
  17. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Look around on MSDN.microsoft.com.
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64

    Smile


    Originally Posted by sam_kh918
    Ok I know I'm pushing it now! but now that I am able to open a program on my Windows computer, is there any way to make that program open a file in it?
    For example I have been able to write a python script that opens a Windows program called LaserGene, now can I also tell my script to open a file from my computer in this LaserGene?
    I have done it with an image viewer this way:
    Code:
    #os.system("c:/python24/ImageViewer.exe %s" % ImageFilename)
  20. #11
  21. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    You might want to remove that # else it'll be a comment.
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Gent, Belgium
    Posts
    19
    Rep Power
    0
    It depends what arguments the executable of lasergene takes.
    I've done it with Word once, it looked like:
    Code:
    os.system("C:\path_to_word\winword.exe /open filename")

IMN logo majestic logo threadwatch logo seochat tools logo