从列表中删除项目 [英] Remove items from a list

查看:75
本文介绍了从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取目录中的文件列表并删除除.dbf之外的所有文件。文件。我使用以下内容尝试删除项目,但他们不会删除。任何帮助将不胜感激。


x = 0
$ _ b $ b每个_dbases:

如果每个[-4:]< ;> " .dbf":

del每个#也尝试过:del _dbases [x]

x = x + 1


我一定是做错了什么,但它就好像是...... ....


签名

.. 。 。 。在我的绳子尽头....!

---

外发邮件已通过无病毒验证。

由AVG反查病毒系统( http://www.grisoft.com)

版本:6.0.749 /病毒库:501 - 发布日期:9/1/04

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

x = 0
for each in _dbases:
if each[-4:] <> ".dbf":
del each # also tried: del _dbases[x]
x = x + 1

I must be doing something wrong, but it acts as though it is....

signed
.. . . . . at the end of my rope....!
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.749 / Virus Database: 501 - Release Date: 9/1/04

推荐答案

_dbases的内容是什么?你如何创建该列表?如果我正确理解,这是一个文件名列表,你可能已经获得了

和os.listdir()。


我想确保我理解这个问题。你是想从列表中删除

这些名字还是你试图自己删除这些文件?

只是确保它不是后者......


你可以在你的例子中加入更多,我可以运行的东西和

看到结果吗?


Dan


" Stan Cook" < SC *** @ elp.rr.com>在消息中写道

新闻:ys ******************* @ fe2.texas.rr.com ...

我试图获取目录中的文件列表并删除除

" .dbf"之外的所有文件。文件。我使用以下内容尝试删除这些项目,但是他们不会删除
。任何帮助将不胜感激。


x = 0
$ _ b $ b每个_dbases:

如果每个[-4:]< ;> " .dbf":

del每个#也尝试过:del _dbases [x]

x = x + 1


我一定是做错了什么,但它就好像是...... ....


签名

.. 。 。 。在我的绳子尽头....!

---

外发邮件已通过无病毒验证。

由AVG反查病毒系统( http://www.grisoft.com)

版本:6.0.749 /病毒数据库:501 - 发布日期:9/1/04
What is the content of _dbases? How do you create that list? If I
understand correctly, it is a list of file names that you may have gotten
with os.listdir( ).

And I want to make sure I understand the problem. Are you trying to remove
the names from the list or are you trying to remove the files themselves?
Just making sure that it''s not the latter...

Can you put more in your example, something that I may be able to run and
see the results?

Dan

"Stan Cook" <sc***@elp.rr.com> wrote in message
news:ys*******************@fe2.texas.rr.com...
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 appreciated.

x = 0
for each in _dbases:
if each[-4:] <> ".dbf":
del each # also tried: del _dbases[x]
x = x + 1

I must be doing something wrong, but it acts as though it is....

signed
.. . . . . at the end of my rope....!
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.749 / Virus Database: 501 - Release Date: 9/1/04


是的,我使用了listdir。该列表是

目录中的文件列表。我想过滤所有内容,但是.dbf

文件。

Dan Perl < DP *** @ rogers.com>在消息中写道

新闻:KO ************** @ news04.bloor.is.net.cable.ro gers.com ...

:_dbases的内容是什么?你如何创建

列表?如果我

:正确理解,这是一个文件名列表,你可以获得

:使用os.listdir()。



:我想确保我理解这个问题。你是否想要删除

:列表中的名字还是你想要自己删除

文件?

:确保它不是后者...



:你能在你的例子中加入更多,我可能会

能跑和

:看结果?



:Dan



:Stan Cook < SC *** @ elp.rr.com>在消息中写道

:news:ys ******************* @ fe2.texas.rr.com ...

:我试图获取一个目录中的文件列表和

除了

之外的所有文件:" .dbf"文件。我使用以下内容尝试删除

项目,但是他们

:不会删除。任何帮助将不胜感激。



:x = 0

:每个_dbases:

:如果每个[-4:]<> " .dbf":

:del每个#也尝试过:del

_dbases [x]

:x = x + 1



:我一定做错了什么,但就好像它是b / b $ b $ ....



:已签署

:. 。 。 。 。在我的绳子尽头....!





:---

:发送邮件经过无病毒认证。

:由AVG反病毒系统检查( http://www.grisoft.com)

:版本:6.0.749 /病毒数据库:501 - 发布日期:

9/1 / 04





---

外发邮件已通过无病毒验证。

由AVG反病毒系统检查( http://www.grisoft.com)

版本:6.0.749 /病毒数据库:501 - 发布日期:

9/1/04
Yes, I used the listdir. The list is a list of files in the
directory. I want to filter everything out but the ".dbf"
files.
"Dan Perl" <dp***@rogers.com> wrote in message
news:KO**************@news04.bloor.is.net.cable.ro gers.com...
: What is the content of _dbases? How do you create that
list? If I
: understand correctly, it is a list of file names that you
may have gotten
: with os.listdir( ).
:
: And I want to make sure I understand the problem. Are you
trying to remove
: the names from the list or are you trying to remove the
files themselves?
: Just making sure that it''s not the latter...
:
: Can you put more in your example, something that I may be
able to run and
: see the results?
:
: Dan
:
: "Stan Cook" <sc***@elp.rr.com> wrote in message
: news:ys*******************@fe2.texas.rr.com...
: 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 appreciated.
:
: x = 0
: for each in _dbases:
: if each[-4:] <> ".dbf":
: del each # also tried: del
_dbases[x]
: x = x + 1
:
: I must be doing something wrong, but it acts as though it
is....
:
: signed
: . . . . . at the end of my rope....!
:
:
: ---
: Outgoing mail is certified Virus Free.
: Checked by AVG anti-virus system (http://www.grisoft.com).
: Version: 6.0.749 / Virus Database: 501 - Release Date:
9/1/04
:
:
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.749 / Virus Database: 501 - Release Date:
9/1/04

Stan Cook写道:
Stan Cook wrote:
我试图获取目录中的文件列表并删除除.dbf之外的所有文件。文件。
I was trying to take a list of files in a directory and remove all but the ".dbf" files.




假设变量files是文件列表:


files = [文件中fname的fname,如果fname.endswith(''。dbf'')]



Assuming the variable "files" is the list of files:

files = [fname for fname in files if fname.endswith(''.dbf'')]


这篇关于从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆