Python Forum
Return all Path value from function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Return all Path value from function (/thread-16867.html)



Return all Path value from function - Palerm0_24 - Mar-18-2019

Hi,
I would like to know how to retrieve all the values of my loop via a Return. In my first function, I loop to recover all my folders, subfolders. Then I go back to pathFiles

In my second function, I test my linux command on all files in folders, but the problem is this: my function only tests the last result of my loop and not all values.

from ftplib import FTP
import ftplib
import os
import errno
import time

class ProcessFile:
  path= "FolderFiles"
  target=" /var/www/folder/Output"
  extract=""
  monitoring=""
  bash="unrar "

  @staticmethod
  def returnPath():
    pathFiles= []
    for root, dirs, files in os.walk(ProcessFile.path, topdown=True):
      for name in dirs:
        os.path.join(root, name)
      for name in files:
        pathFiles= os.path.join(root, name)
        print(pathFiles)
    return pathFiles 

  @staticmethod
  def testArchive(pathFile):
    monitoring = ProcessFile.bash+"t "+pathFile
    verifyFiles = os.system(monitoring)
    return verifyFiles 

def testCodeRetour():
  myPath = ProcessFile.returnPath()
  print(myPath)
Do you have any idea how it works?

Thank you for your help


RE: Return all Path value from function - nilamo - Mar-18-2019

If a list you want to return, then a list you must construct.

all_paths = []
for name in files:
    pathFiles = os.path.join(root, name)
    print(pathFiles)
    all_paths.append(pathFiles)
    # OR: all_paths.extend(pathFiles)
return all_paths



RE: Return all Path value from function - ichabod801 - Mar-18-2019

That's because you are not returning a list. Line 21 overwrites your list with a single value. I expect you want to append that value to the list, not assign it to the same name.