python - 从函数返回所有路径值

标签 python for-loop path

我想知道如何通过 Return 检索循环的所有值。 在我的第一个函数中,我循环恢复所有文件夹、子文件夹。然后我回到pathFiles

在我的第二个函数中,我在文件夹中的所有文件上测试我的 linux 命令,但问题是:我的函数仅测试循环的最后结果而不是所有值。

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)

你知道它是如何工作的吗?

感谢您的帮助

最佳答案

列表pathFiles= []从未被使用过。也许你想在其中添加一些东西?如果是这种情况,那么您必须修复一些问题。

在循环中:

for name in files:
    pathFiles= os.path.join(root, name)
    print(pathFiles)

将名称 pathFiles 更改为 pathFile,然后将其附加到列表中。

for name in files:
    pathFile= os.path.join(root, name)
    print(pathFile)
    pathFiles.append(pathFile)

关于python - 从函数返回所有路径值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55223911/

相关文章:

python - opencv保存过滤图像

c# - For循环计算阶乘

docker-compose 在主机重启后启动容器。哪个?

python - Nexus pypi 存储库 "Could not find a version that satisfies the requirement"

python - 在 Python 中导入模块的文件夹

python - 如何按数字排序但使用 dtype=object 读取_csv?

c# - for 循环内的动态标签文本

c - 函数无法正确添加二维数组的元素?

java - 在 WSL 和 Windows 上安装 SDK(JDK、Maven、Gradle)?

linux - 在 linux 中为同一程序的两个不同版本设置别名?