I want to search all the subfolder files and make them into files, but the print and append values are different.

Asked 2 years ago, Updated 2 years ago, 84 views

I'm going to create a project where I search all the subfolder files and make them into files The value comes out well if you print it, but when I added it to the list by append and returned the list, the values in print came out well It's not in here.

import os
from tkinter import filedialog
from tkinter import *

class FileList:

    def FolderOpen(self):
        window = Tk().withdraw()
        dirName=filedialog.askdirectory(initialdir="/",title='Please select a directory to check.')
        return dirName

    def FileSave(self):
        window = Tk().withdraw()
        title = 'Save File'
        ftypes = [('txt FIle', '.txt'), ('All files', '*')]
        filename = filedialog.asksaveasfilename(filetypes=ftypes, title=title, initialfile='FileSearch.txt')
        return filename

    def search(self, dirname):
        files = list()
        try:
            filenames = os.listdir(dirname)
            for filename in filenames:
                full_filename = os.path.join(dirname, filename)
                if os.path.isdir(full_filename):
                    self.search(full_filename)
                else:
                    print(full_filename)
                    files.append(full_filename)
            return files
        except PermissionError:
            pass


File = FileList()
FolderFind = File.FolderOpen()
filename = File.FileSave()
file = File.search(FolderFind)
print(file)

The values of the printed and appended lists are different. I referred to Jo Coding's code.

print append python

2022-09-20 18:55

1 Answers

The files appear to be initialized each time the function is executed.

If there are no other factors that affect the list, why don't you take the list out of the function?

import os
from tkinter import filedialog
from tkinter import *

files = list()

class FileList:

    def FolderOpen(self):
        window = Tk().withdraw()
        dirName=filedialog.askdirectory(initialdir="/",title='Please select a directory to check.')
        return dirName

    def FileSave(self):
        window = Tk().withdraw()
        title = 'Save File'
        ftypes = [('txt FIle', '.txt'), ('All files', '*')]
        filename = filedialog.asksaveasfilename(filetypes=ftypes, title=title, initialfile='FileSearch.txt')
        return filename

    def search(self, dirname):
        #files = list()
        try:
            filenames = os.listdir(dirname)
            for filename in filenames:
                full_filename = os.path.join(dirname, filename)
                if os.path.isdir(full_filename):
                    self.search(full_filename)
                else:
                    print(full_filename)
                    files.append(full_filename)
            return files
        except PermissionError:
            pass


File = FileList()
FolderFind = File.FolderOpen()
filename = File.FileSave()
file = File.search(FolderFind)
print(file)


2022-09-20 18:55

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.