python读取文件名存到list_python-read文件名并构建名称列表(python-read file names and build a namelist)...

python-read文件名并构建名称列表(python-read file names and build a namelist)

我想创建一个名单并将所有名称存储在四个文件夹中。 我建立

namelist = {1:[], 2:[], 3:[], 4:[]}

在方法中,我写

for file_name in sorted(os.listdir(full_subdir_name)):

full_file_name = os.path.join(full_subdir_name,file_name)

#namelist[level] += blabla...

我想将第一个文件夹中的名称添加到namelist [1],从第二个文件夹添加到namelist [2]。 我不知道如何将不同级别的所有名称添加到其中。 谢谢!

I want to make a name list and store all the names in four folders. I build

namelist = {1:[], 2:[], 3:[], 4:[]}

In the method, I write

for file_name in sorted(os.listdir(full_subdir_name)):

full_file_name = os.path.join(full_subdir_name,file_name)

#namelist[level] += blabla...

I want to add the names from the first folder into namelist[1], from the second folder to namelist[2]. I don't know how I can add all the names in different levels to it. Thx!

原文:https://stackoverflow.com/questions/10158042

2020-02-21 18:01

满意答案

我不完全确定这是你对上述问题的看法。 但似乎首先,您希望使用enumerate()来保留四个文件夹的索引和名称。 但是,我认为你实际上需要一个额外的for循环,根据你上面的评论,实际附加每个子文件夹中每个文件的所有内容。 以下应该可以解决问题。

另请注意,您的字典键以1开头,因此您需要考虑枚举索引以0开头的事实。

# Here, enumerate gives back the index and element.

for i,file_name in enumerate(sorted(os.listdir(full_subdir_name))):

full_file_name = os.path.join(full_subdir_name,file_name)

# Here, 'elem' will be the strings naming the actual

# files inside of the folders.

for elem in sorted(os.listdir(full_file_name)):

# Here I am assuming you don't want to append the full path,

# but you can easily change what to append by adding the

# whole current file path: os.path.join(full_file_name, elem)

namelist[i+1].append(elem)

I am not totally sure this is what you are getting at with your question above. But it seems that first, you want to use enumerate() to allow you to keep the index and name of the four folders. But then, I think you will actually need an additional for-loop, based on your comments above, to actually append all of the contents for each of the files within each sub-folder. The following should do the trick.

Also note that your dictionary keys start with 1, so you need to account for the fact that the enumerate index starts with 0.

# Here, enumerate gives back the index and element.

for i,file_name in enumerate(sorted(os.listdir(full_subdir_name))):

full_file_name = os.path.join(full_subdir_name,file_name)

# Here, 'elem' will be the strings naming the actual

# files inside of the folders.

for elem in sorted(os.listdir(full_file_name)):

# Here I am assuming you don't want to append the full path,

# but you can easily change what to append by adding the

# whole current file path: os.path.join(full_file_name, elem)

namelist[i+1].append(elem)

2012-04-15

相关问答

下面是一个简单的例子,它可以让你从C中读取Fortran名单。我使用了你在问题input.txt提供的名称列表文件。 Fortran子程序nmlread_f.f90 (注意使用ISO_C_BINDING ): subroutine namelistRead(n,m,l) bind(c,name='namelistRead')

use,intrinsic :: iso_c_binding,only:c_float,c_int

implicit none

real(kind=c_flo...

您可能可以使用pathconvert任务 ,该任务允许您将分隔符指定为逗号。

说你正在阅读的文件是: &INT_NAMELIST

A = 1,

B = 2

/

&REAL_NAMELIST

X = 3.15,

Y = 2.71

/

然后程序 INTEGER :: A, B

REAL :: X, Y

NAMELIST /INT_NAMELIST/ A, B

NAMELIST /REAL_NAMELIST/ X, Y

OPEN(unit=100, action="READ", status="OLD")

read(100, nml=INT_NAMELIST)

...

哇,谢谢你的问题 - 之前从未听过名单:)这很有用!! :)经过一些测试,较旧的gfortran版本存在问题。 让我们说你有 program nltest

implicit none

integer :: a(3,3)

namelist /mylist/ a

a = 0

open(7, file='nlinput.txt')

read(7, nml = mylist)

write(*,*) a

end program nltest

读取一个完整的数组, a=1,2,3...

简而言之:namelist没有类型,因为它是一个语句,而不是一个变量声明。 这意味着它的使用非常有限:仅作为I / O操作的nml =参数。 它也是一个非常古老的功能,如此处所述,其功能自引入以来几乎没有变化。 因此,最直接的取决于您想要做什么。 例如,您可以尝试将一个名称列表用于多种用途,或者设计您自己的输入文件格式,您可以使用自定义读取例程。 编辑: 假设您有几个模块中的抽象类型和一些扩展: module absTypeMod

implicit none

type, abstract...

请看我对你上一个问题的回答 ; 它提供了如何使用名单的详细信息。 至于为什么要使用名单:namelist将键/值对传递给您通过访问的Web服务。 与任何其他Web服务一样,Web服务可以使用这些键/值对来决定发送给您的响应。 作为一个具体示例,Web服务可以动态生成VoiceXML,其中包含基于其接收的值的annoucenements和options。 Please see my answer to your previous question; it gives details ...

这看起来像编译器错误。 假设阵列grid是形状 ,而不是假设的大小 。 从F2003开始,在名单中允许假设的形状数组,假设大小数组仍然被禁止(在运行时,假定大小数组的大小不一定是已知的,因此禁止需要知道大小的操作)。 一个简单的解决方法是将伪参数重命名为其他内容,然后将其值复制到本地可分配的命名grid 。 subroutine writeGrid(fname, grid_renamed)

character*(*) :: fname

real, dimension(:,:) :: gri...

我不完全确定这是你对上述问题的看法。 但似乎首先,您希望使用enumerate()来保留四个文件夹的索引和名称。 但是,我认为你实际上需要一个额外的for循环,根据你上面的评论,实际附加每个子文件夹中每个文件的所有内容。 以下应该可以解决问题。 另请注意,您的字典键以1开头,因此您需要考虑枚举索引以0开头的事实。 # Here, enumerate gives back the index and element.

for i,file_name in enumerate(sorted(os.li...

我没有得到确切的原因,但至少我找到了一个解决方法,玩代码。 如果名单中读取的参数在子程序中是私有的,则可以毫无问题地读取它们; 因此取代 call read_nml

通过 call read_nml(nx, ny, nz)

和子程序read_nml by subroutine read_nml(nx, ny, nz)

implicit none

integer :: nx, ny, nz

namelist /input_params/ nx...

相关文章

1. install software Cygwin, jdk, ant, nutch 2. conf

...

ZOJ Problem Set - 2966 Build The Electric Sys

...

1.安装mvn 2.下载源代码 3.build mvn package 过程中出现问题,clojars

...

今天再一次配置HDFS,决定记录下来以备不时之需。 首先你的电脑需要安装上java JDK 1.6 这

...

Data Week: Becoming a data scientist Data Pointed,

...

在自己虚拟机上使用mysql作为hive元数据存储, 修改配置如下: <pro

...

Java 流(Stream)、文件(File)和IO Java.io包几乎包含了所有操作输入、输

...

http://gumstix.org/create-a-bootable-microsd-card.h

...

With an executive staffing venture about to open, a

...

PHP 中dirname(_file_) 2007-5-3 16:00|查看: 19256|评论:

...

最新问答

如果启用了复制处理程序,请确保将其置于其中一个安全角色之后。 我见过人们做的另一件事是在不同的端口上运行admin。 最好在需要auth的页面上使用SSL,这样你就不会发送明确的密码,因此管理和复制将发生在8443上,而常规查询将在8080上发生。 如果您要签署自己的证书,请查看此有用的SO页面: 如何在特定连接上使用不同的证书? I didn't know that /admin was the context for SOLR admin because /admin does not re

第一:在您的样本中,您有: 但是你在询问 //td[@class=‘CarMiniProfile-TableHeader’] (注意TableHeader中的大写'T')。 xpath区分大小写。 第二:通过查询// td [@ class ='CarMiniProfile-TableHeader'] / td,你暗示你在外部td中有一个'td'元素,而它们是兄弟姐妹。 有很多方法可以在这里获得制作和模型

这是你的答案: http://jsfiddle.net/gPsdk/40/ .preloader-container { position: absolute; top: 0px; right: 0px; bottom: 0px; left: 0px; background: #FFFFFF; z-index: 5; opacity: 1; -webkit-transition: all 500ms ease-out;

问题是,在启用Outlook库引用的情况下, olMailItem是一个保留常量,我认为当您将Dim olMailItem as Outlook.MailItem ,这不是问题,但是尝试设置变量会导致问题。 以下是完整的解释: 您已将olMailItem声明为对象变量。 在赋值语句的右侧,在将其值设置为对象的实例之前,您将引用此Object 。 这基本上是一个递归错误,因为你有对象试图自己分配自己。 还有另一个潜在的错误,如果之前已经分配了olMailItem ,这个语句会引发另一个错误(可能是

我建议使用wireshark http://www.wireshark.org/通过记录(“捕获”)设备可以看到的网络流量副本来“监听”网络上发生的对话。 当您开始捕获时,数据量似乎过大,但如果您能够发现任何看起来像您的SOAP消息的片段(应该很容易发现),那么您可以通过右键单击并选择来快速过滤到该对话'关注TCP Stream'。 然后,您可以在弹出窗口中查看您编写的SOAP服务与Silverlight客户端之间的整个对话。 如果一切正常,请关闭弹出窗口。 作为一个额外的好处,wireshar

Android默认情况下不提供TextView的合理结果。 您可以使用以下库并实现适当的aligntment。 https://github.com/navabi/JustifiedTextView Android Does not provide Justified aligntment of TextView By default. You can use following library and achieve proper aligntment. https://github.com/

你的代码适合我: class apples { public static void main(String args[]) { System.out.println("Hello World!"); } } 我将它下载到c:\ temp \ apples.java。 以下是我编译和运行的方式: C:\temp>javac -cp . apples.java C:\temp>dir apples Volume in drive C is HP_PAV

12个十六进制数字(带前导0x)表示48位。 那是256 TB的虚拟地址空间。 在AMD64上阅读wiki(我假设你在上面,对吗?)架构http://en.wikipedia.org/wiki/X86-64 12 hex digits (with leading 0x) mean 48 bits. That is 256 TB of virtual address space. Read wiki on AMD64 (I assume that you are on it, right?) ar

这将取决于你想要的。 对象有两种属性:类属性和实例属性。 类属性 类属性对于类的每个实例都是相同的对象。 class MyClass: class_attribute = [] 这里已经为类定义了MyClass.class_attribute ,您可以使用它。 如果您创建MyClass实例,则每个实例都可以访问相同的class_attribute 。 实例属性 instance属性仅在创建实例时可用,并且对于类的每个实例都是唯一的。 您只能在实例上使用它们。 在方法__init__中定

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值