1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2013
    Rep Power

    Question Copying path from command prompt

    Hi ..

    I got a scenario where I have to copy the path name to a argument or variable (eg: D:\dev\python\file.py) of a file directory from command prompt only. Lets say python folder has 50 or more python files. I want to delete the files which has "virus" word in those 50 python files. from the python folder.

    How can i work on this can anyone please help me !!

    Thanks indeed
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Rep Power
    The 'glob' module is the easiest to pull in files by name with wildcard matching:

    >>> import glob
    >>> glob.glob('c:\python27\*.exe')
    ['c:\\python27\\python.exe', 'c:\\python27\\pythonw.exe', 'c:\\python27\\Removepywin32.exe']
    Or use os.listdir() to get all files (but you have to add the base path to the result yourself):

    import os
    mypath = 'c:\\python27'
    for f in os.listdir(mypath):
        print os.path.join(mypath, f)
    Use os.remove() to delete files.

    Also, see: http://stackoverflow.com/questions/120656/directory-listing-in-python

IMN logo majestic logo threadwatch logo seochat tools logo