473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

remove item from list based on name

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
Jul 18 '05 #1
5 2610
"Bart Nessux" <ba*********@hotmail.com> wrote in message
news:c9**********@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?


Have you tried this?

for name in systemfiles:
filelist.remove(name)
--
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.
Jul 18 '05 #2
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" <ba*********@hotmail.com> wrote in message
news:c9**********@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

Jul 18 '05 #3
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

Jul 18 '05 #4
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 Otten wrote:
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

Jul 18 '05 #5
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" <ba*********@hotmail.com> wrote in message
news:40**************@hotmail.com...
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 Otten wrote:
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

Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
3
by: Don | last post by:
My user control has a combobox with an arraylist attached to it along with custom add and remove methods. The "Add" method is working great. However I don't understand why the "Remove" method...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
2
by: Kela | last post by:
An interesting problem: I have a ListView with LabelEdit set to TRUE. When I change the label, I want to make some decisions as to whether the ListViewItem (that's just been edited) should stay in...
10
by: pamelafluente | last post by:
Hi I have a sorted list with several thousands items. In my case, but this is not important, objects are stored only in Keys, Values are all Nothing. Several of the stored objects (might be a...
4
by: Jerad Rose | last post by:
I'm baffled by this -- is there not a typed object used for ComboBox Items? Best I can tell, all of the methods for ComboBox that accept an Item are of type Object. Why in the world is a...
4
by: Yansky | last post by:
Got a quick n00b question. What's the difference between del and remove?
2
by: Steve | last post by:
I am working on a program that works like a check in/check out system. There is a folder on a network drive that stores a bunch of vb programs. This program will check in and check out programs...
10
by: Tony Johansson | last post by:
Hello! I have a collection with a lot of object. Each object has the following definition. public class ParametermappingObj { private string Name {get;set;} private string Id {get;set;}...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.