remove item from list based on name

B

Bart Nessux

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
 
R

Russell Blau

Bart Nessux said:
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?

Have you tried this?

for name in systemfiles:
filelist.remove(name)
 
P

Peter Otten

Bart said:
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?
from sets import Set
list(Set(["a", "b", "c"]) - Set(["c", "d"]))
['a', 'b']

Remember to pass all filenames through os.path.normcase() before trying the
above.

Peter
 
L

Larry Bates

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.
 
B

Bart Nessux

Thanks Peter & Larry,

Both of the examples work (lists and sets). I had forgotten that I had
done something similar in the past with both functions. I find the set
approach cleaner, but both approaches get the job done.

Thanks Again!


Peter said:
Bart Nessux wrote:

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?
from sets import Set
list(Set(["a", "b", "c"]) - Set(["c", "d"]))

['a', 'b']

Remember to pass all filenames through os.path.normcase() before trying the
above.

Peter
 
L

Larry Bates

I'm glad they worked for you.

I tend to stay away from V2.3 "only" solutions, but
I guess it's probably time I started recommending
using these new features.

-Larry
Bart Nessux said:
Thanks Peter & Larry,

Both of the examples work (lists and sets). I had forgotten that I had
done something similar in the past with both functions. I find the set
approach cleaner, but both approaches get the job done.

Thanks Again!


Peter said:
Bart Nessux wrote:

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?

from sets import Set
list(Set(["a", "b", "c"]) - Set(["c", "d"]))

['a', 'b']

Remember to pass all filenames through os.path.normcase() before trying the
above.

Peter
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top