简体   繁体   English

Python 只打印一个文件

[英]Python printing only one file

Can someone explain me, why my function out() prints only one file?有人可以解释一下,为什么我的 function out() 只打印一个文件? how can i fix, bcs it should print a lot of files recursively from 2019 DIR我该如何解决,bcs它应该从2019 DIR递归打印很多文件

class log():

    def __init__(self, search):
        self.search = search

    def get_files(self):
        path = '/var/log/HOSTS/dhcpd-s/2019'
        for root, directories, filenames in os.walk(path):
            for directory in directories:
                #do whatever u want with absolute dirs path
                dir_path = os.path.join(root, directory)

            for filename in filenames:
                #do whatever u want with absolute file path
                file_path = os.path.join(root, filename)
                if os.path.getsize(file_path) == 0:
                    pass
                else:
                    self.file_path = file_path

    def out(self):
        print(self.file_path)


if __name__=='__main__':
   p = log(search = sys.argv[1])
   p.get_files()
   p.out()

You are running in a loop您正在循环运行

self.file_path = file_path

In each iteration it assigns new file path and after exiting the loop it holds the value of last iteration.在每次迭代中,它分配新的文件路径,并在退出循环后保存上次迭代的值。

You can declare self.file_path as a list您可以将self.file_path声明为list

def __init__(self, search):
        self.search = search
        self.file_path = []

And append file_path into the list和 append file_path进入列表

self.file_path.append(file_path)

Now you can print all paths现在您可以打印所有路径

def out(self):
    for path  in self.file_path:
        print(path)

This should work这应该工作

class log():

    def __init__(self, search):
        self.search = search

    def get_files(self):
        files = []
        path = '/var/log/HOSTS/dhcpd-s/2019'
        for root, directories, filenames in os.walk(path):
            for directory in directories:
                #do whatever u want with absolute dirs path
                dir_path = os.path.join(root, directory)

            for filename in filenames:
                #do whatever u want with absolute file path
                file_path = os.path.join(root, filename)
                if os.path.getsize(file_path) == 0:
                    pass
                else:
                    files.append(file_path)

         self.files_path = files
    def out(self):
        print(self.file_path)


if __name__=='__main__':
   p = log(search = sys.argv[1])
   p.get_files()
   p.out()

Below (collect data into a list)下面(将数据收集到列表中)

import os 
import sys

class log():

    def __init__(self, search):
        self.search = search
        self.file_path = []

    def get_files(self):
        path = '/var/log/HOSTS/dhcpd-s/2019'
        for root, directories, filenames in os.walk(path):
            for directory in directories:
                #do whatever u want with absolute dirs path
                dir_path = os.path.join(root, directory)

            for filename in filenames:
                #do whatever u want with absolute file path
                file_path = os.path.join(root, filename)
                if os.path.getsize(file_path) == 0:
                    pass
                else:
                    self.file_path.append(file_path)

    def out(self):
        print(self.file_path)


if __name__=='__main__':
   p = log(search = sys.argv[1])
   p.get_files()
   p.out()

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM