gpt4 book ai didi

Python 的 re.findAll() 函数无法按预期工作

转载 作者:太空宇宙 更新时间:2023-11-04 09:42:50 25 4
gpt4 key购买 nike

我正在尝试创建一个 python 脚本,它将从工作目录中找到具有特定名称模式的所有文件。

我已将所有文件存储在一个列表中,然后尝试在列表上应用 re.findall 方法以仅获取具有该名称模式的文件列表。

我写了这段代码:

# Create the regex object that we will use to find our files
fileRegex = re.compile(r'A[0-9]*[a-z]*[0-9]*.*')
all_files = []

# Recursevly read the contents of the working_dir/Main folder #:
for folderName, subfolders, filenames in os.walk(working_directory + "/Main"):
for filename in filenames:
all_files.append(filename)

found_files = fileRegex.findall(all_files)

我在代码的最后一行收到这个错误:

TypeError: expected string or bytes-like object

我也尝试过 re.findall(all_files) 而不是使用在该行之前创建的“fileRegex”。同样的错误。请告诉我我做错了什么。非常感谢您阅读我的帖子!

编辑(第二个问题):我已经按照答案进行操作,现在工作正常。找到文件后,我正在尝试创建一个包含与该模式匹配的文件的存档。存档是创建的,但是我编写代码的方式是文件的整个路径都包含在存档中(从/到文件的所有文件夹)。我只希望文件包含在最终的 .zip 中,而不是构成它路径的整个目录和子目录。

这是代码。 .zip 文件的生成在底部。请给我一个提示,我该如何解决这个问题我已经尝试了很多但都没有用。谢谢:

# Project properties:

# Recursively read the contents of the 'Main' folder which contains files with different names.
# Select only the files whose name begin with letter A and contain digits in it. Use regexes for this.
# Archive these files in a folder named 'Created_Archive' in the project directory. Give the archive a name of your choosing.


# Files that you should find are:
# Aerials3.txt, Albert0512.txt, Alberto1341.txt


########################################################################################################################################
import os
import re
import zipfile
from pathlib import Path

# Get to the proper working directory
working_directory = os.getcwd()
if working_directory != "/home/paul/Desktop/Python_Tutorials/Projects/Files_And_Archive":
working_directory = "/home/paul/Desktop/Python_Tutorials/Projects/Files_And_Archive"
os.chdir(working_directory)

check_archive = Path(os.getcwd() + "/" + "files.zip")
if check_archive.is_file():
print("Yes. Deleting it and creating it.")
os.unlink(os.getcwd() + "/" + "files.zip")
else:
print("No. Creating it.")

# Create the regex object that we will use to find our files
fileRegex = re.compile(r'A[0-9]*[a-z]*[0-9]+.*')
found_files = []

# Create the zipfile object that we will use to create our archive
fileArchive = zipfile.ZipFile('files.zip', 'a')

# Recursevly read the contents of the working_dir/Main folder #:
for folderName, subfolders, filenames in os.walk(working_directory + "/Main"):
for filename in filenames:
if fileRegex.match(filename):
found_files.append(folderName + "/" + filename)

# Check all files have been found and create the archive. If the archive already exists
# delete it.


for file in found_files:
print(file)
fileArchive.write(file, compress_type=zipfile.ZIP_DEFLATED)

fileArchive.close()

最佳答案

re.findAll适用于不在列表中的字符串,因此最好使用 r.match在列表上过滤实际匹配的那些:

found_files = [s for s in all_files if fileRegex.match(s)]

关于Python 的 re.findAll() 函数无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51000342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com