【问题标题】:Return all Path value from function从函数返回所有路径值
【发布时间】:2019-03-18 14:41:09
【问题描述】:

我想知道如何通过 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)

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

感谢您的帮助

【问题讨论】:

标签: python for-loop path


【解决方案1】:

列表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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多