简体   繁体   English

Python:在目录中查找文件但忽略文件夹及其内容

[英]Python: Finding files in directory but ignoring folders and their contents

So my program search_file.py is trying to look for.log files in the directory it is currently placed in. I used the following code to do so:所以我的程序 search_file.py 正在尝试在它当前所在的目录中查找 .log 文件。我使用以下代码来执行此操作:

import os

# This is to get the directory that the program is currently running in
dir_path = os.path.dirname(os.path.realpath(__file__))

# for loop is meant to scan through the current directory the program is in
for root, dirs, files in os.walk(dir_path):
    for file in files:
        # Check if file ends with .log, if so print file name
        if file.endswith('.log')
            print(file)

My current directory is as follows:我当前目录如下:

search_file.py搜索文件.py

sample_1.log示例_1.log

sample_2.log样本_2.log

extra_file (this is a folder) extra_file(这是一个文件夹)

And within the extra_file folder we have:在 extra_file 文件夹中,我们有:

extra_sample_1.log extra_sample_1.log

extra_sample_2.log extra_sample_2.log

Now, when the program runs and prints the files out it also takes into account the.log files in the extra_file folder.现在,当程序运行并打印出文件时,它还会考虑 extra_file 文件夹中的 .log 文件。 But I do not want this.但我不想要这个。 I only want it to print out sample_1.log and sample_2.log.我只希望它打印出 sample_1.log 和 sample_2.log。 How would I approach this?我将如何处理这个?

Try this:尝试这个:

import os

files = os.listdir()

for file in files:
    if file.endswith('.log'):
        print(file)

The problem in your code is os.walk traverses the whole directory tree and not just your current directory.您的代码中的问题是os.walk遍历整个目录树而不仅仅是您的当前目录。 os.listdir returns a list of all filenames in a directory with the default being your current directory which is what you are looking for. os.listdir返回目录中所有文件名的列表,默认情况下是您正在寻找的当前目录。

os.walk documentation os.walk 文档

os.listdir documentation os.listdir 文档

By default, os.walk does a root-first traversal of the tree, so you know the first emitted data is the good stuff.默认情况下, os.walk对树进行根优先遍历,因此您知道第一个发出的数据是好东西。 So, just ask for the first one.所以,只要求第一个。 And since you don't really care about root or dirs, use _ as the "don't care" variable name由于您并不真正关心 root 或 dirs,因此使用_作为“不关心”变量名

# get root files list.
_, _, files = next(os.walk(dir_path))
for file in files:
    # Check if file ends with .log, if so print file name
    if file.endswith('.log')
        print(file)

Its also common to use glob:使用 glob 也很常见:

from glob import glob
dir_path = os.path.dirname(os.path.realpath(__file__))
for file in glob(os.path.join(dir_path, "*.log")):
    print(file)

This runs the risk that there is a directory that ends in ".log", so you could also add a testing using os.path.isfile(file) .这存在以“.log”结尾的目录的风险,因此您还可以使用os.path.isfile(file)添加测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flask 在 Python 中找不到文件夹和文件 - Flask is not finding folders and files in Python 使用Python将文件从目录复制到文件夹 - Copy Files from Directory to Folders using Python 使用 python 在目录中搜索文件夹和文件 - Searching a directory for folders and files using python 在目录中查找重复文件和文件夹并将重复项移动到 python 中的不同文件夹 - Find duplicates files and folders in directory and move the duplicates to different folders in python 组织文件时忽略文件夹 - Ignoring folders when organizing files 读取 python 中当前目录下所有文件的内容 - Reading the contents of all files in current directory in python Python递归删除目录中的文件/文件夹,但不删除父目录或特定文件夹 - Python recursively remove files/folders in a directory, but not the parent directory or a specific folder 在python目录中递归查找文件md5 - Finding md5 of files recursively in directory in python Python-遍历目录中的子文件夹和文件,而不会忽略该子文件夹 - Python - loop through subfolders and files in a directory without ignoring the subfolder Python - 将文件夹从一个目录复制到另一个目录(仅复制我包含在 txt 文件中的文件夹) - Python - Copy folders from one directory to another directory (Copy only that folders that i have included in a txt files)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM