在Jupyter目录中循环并将文件名添加到列表中

2024-05-14 21:41:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的文件设置(大约15.xlsx文件在一个名为file的较大文件中,位于Jupyter的主目录中)。我想循环浏览所有以字母组合开头的文件,然后将这些文件名添加到列表中。这就是我目前所拥有的。我想知道:1。正确的文件路径名是什么?2.如何返回所需的输出

import os

directory = '???/'   <--- to find this enter pwd into cell

file_name_list = []

for filename in os.listdir(directory):
    if filename.startswith("SOME_LETTERS"):
        file_name_list.append(filename)
    else:
        continue

文件设置示例:

FILE --> 
SOME_LETTERS_1.xlsx 
DIFFERENT_LETTERS_1.xlsx
ONE_NUMBER. xlsx
SOME_LETTERS_2.xlsx
DIFFERENT_LETTERS_2.xlsx
SOME_LETTERS_3.xlsx
SOME_LETTERS_4.xlsx 

期望输出:

[SOME_LETTERS_1, SOME_LETTERS_2, SOME_LETTERS_3, SOME_LETTERS_4] 

Tags: 文件nameos文件名jupytersomefilenamexlsx
1条回答
网友
1楼 · 发布于 2024-05-14 21:41:55

使用glob模块https://docs.python.org/3/library/glob.html

从文档中:

The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched.

以下是一个例子:

from glob import glob

for file in glob("path/to/some/folder/*.txt"):
    print(file)

上面的代码将打印给定文件夹中所有.txt文件的名称

因此,在您的情况下,代码如下所示:

"""
Folder structure:
├── samples
│   ├── Asample2.txt
│   ├── Bsample4.txt
│   ├── sample3.txt
│   ├── sample5.txt
│   └── sample.txt
└── stack.py
"""

from glob import glob
import os

# Using os.path.join so it works on multiple platforms
dir = os.path.join("samples", "*.txt")


 # os.path.basename extracts the file name from the fullpath
file_name_list = [file for file in glob(dir) if os.path.basename(file).startswith("s")]
print(file_name_list)
>>>['samples/sample5.txt', 'samples/sample.txt', 'samples/sample3.txt']

另一种方法是使用Unix扩展:

from glob import glob
import os

# Some letter here:     -
#                             \
#                              v
dir = os.path.join("samples", "s*.txt")

file_name_list = [file for file in glob(dir)]
print(file_name_list)

相关问题 更多 >

    热门问题