Python Forum
Return all Path value from function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return all Path value from function
#1
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
Reply
#2
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
Reply
#3
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  nested function return MHGhonaim 2 562 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,174 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,131 Feb-04-2023, 12:30 PM
Last Post: caslor
  return vs. print in nested function example Mark17 4 1,674 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,149 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  How to invoke a function with return statement in list comprehension? maiya 4 2,751 Jul-17-2021, 04:30 PM
Last Post: maiya
  Function - Return multiple values tester_V 10 4,319 Jun-02-2021, 05:34 AM
Last Post: tester_V
  Get return value from a threaded function Reverend_Jim 3 16,804 Mar-12-2021, 03:44 AM
Last Post: Reverend_Jim
  Return not exiting function?? rudihammad 3 5,190 Dec-01-2020, 07:11 PM
Last Post: bowlofred
  Why does my function return None? vg100h 3 2,156 Oct-29-2020, 06:17 AM
Last Post: vg100h

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020