remove item from list based on name

Larry Bates lbates at swamisoft.com
Thu May 27 16:09:12 EDT 2004


Something like the following:

locked_files=['C:\pagefile.sys',
              'C:\hiberfil.sys',
              'C:\Documents and Settings\All Users\Application
Data\Microsoft\Dr Watson\user.dmp',
              'C:\DOCUMENTS AND SETTINGS\LOCALSERVICE\NTUSER.DAT',
              'C:\DOCUMENTS AND SETTINGS\LOCALSERVICE\ntuser.dat.LOG',
              'C:\DOCUMENTS AND SETTINGS\NETWORKSERVICE\NTUSER.DAT',
              'C:\DOCUMENTS AND SETTINGS\NETWORKSERVICE\ntuser.dat.LOG',
              'C:\WINDOWS\SYSTEM32\CONFIG\default',
              'C:\WINDOWS\SYSTEM32\CONFIG\default.LOG',
              'C:\WINDOWS\SYSTEM32\CONFIG\SAM',
              'C:\WINDOWS\SYSTEM32\CONFIG\SAM.LOG',
              'C:\WINDOWS\SYSTEM32\CONFIG\SECURITY',
              'C:\WINDOWS\SYSTEM32\CONFIG\SECURITY.LOG',
              'C:\WINDOWS\SYSTEM32\CONFIG\software',
              'C:\WINDOWS\SYSTEM32\CONFIG\software.LOG',
              'C:\WINDOWS\SYSTEM32\CONFIG\system',
              'C:\WINDOWS\SYSTEM32\CONFIG\system.LOG']

and your list of files is in your_files, use list comprehension
to build a new_list.

new_list=[f for f in your_files if f not in locked_files]

This gets you a new list of only those files that are not
in the locked_files list.

Note, you must have an exact match (e.g. full pathnames)
for this to work.

HTH,
Larry Bates
Syscon, Inc.


"Bart Nessux" <bart_nessux at hotmail.com> wrote in message
news:c95e2g$nm2$1 at solaris.cc.vt.edu...
> Hi,
>
> I am generating a list of file names... some of the files are locked by
> the OS (Windows XP) and I know the names of these files (NTUSER.DAT,
> ntuser.dat.LOG, etc.) But, I don't know their position in the list. Is
> there a way that I can delete these items from the list based on their
> names?
>
> Thanks,
> Bart





More information about the Python-list mailing list