【问题标题】:Loop doesn't unrar my files... Code doesn't enter into the loop循环不解压缩我的文件...代码不进入循环
【发布时间】:2019-02-13 10:55:16
【问题描述】:

大家早上好,

我的代码有问题。我会解释一下,我在我的 linux 实例上连接到本地 FTP 服务器。我想探索我所有的目录以解压缩我的文件“.rar”,但是当我启动我的脚本时,只显示正在工作的目录。

我不知道为什么。

def showDirectories(ftp):

  output=' /var/www/folder/Output'
  ftp.cwd('/FolderFiles')
  ftp.retrlines('LIST')
  directories = ftp.nlst()

  if directories not in ['..', '.']:
    i=0
    while i < len(directories):
      folder = directories[i]+'/'
      i+=1
      for root, dirs, files in os.walk(folder, topdown=False):
        for name in files:
          rarFiles=os.path.join(root, name)
          print(rarFiles)
          unrar = "unrar x "+rarFiles+output
          print("unrar commande"+str(unrar))
          download= os.system(unrar)
          print(download)
        ftp.cwd('..')
      print("")

结果如下:

[root@ip-10-0-2-52 folder]# python test.py
Connected! Welcome msg is "220 (vsFTPd 3.0.2)"
Connected !

drwxr-xr-x    2 0        0              90 Feb 11 14:42 DescriptiveData
drwxr-xr-x    2 0        0              75 Feb 11 14:42 Financials
drwxr-xr-x    2 0        0              87 Feb 11 14:42 OwnershipHisto

感谢您的帮助

【问题讨论】:

  • "如果目录不在 ['..', '.'];"在上下文中没有意义。
  • 是的,它只是用于测试的调试线。我在评论中做了这一行
  • 您是否尝试在 ftp 端使用os.walk?不确定这是否那么容易;不过看看here

标签: linux python-2.7 ftplib unrar


【解决方案1】:

感谢您的帮助,我终于找到了使我的脚本工作的解决方案。

这是我的更新

def downloadFile(path,target):

  for root, dirs, files in os.walk(path, topdown=False):
    for name in files:
      rarFiles=os.path.join(root, name)
      print(rarFiles)
      unrar = "unrar x "+rarFiles+target
      print("unrar commande"+str(unrar))
      download= os.system(unrar)
      print(download)

downloadFile('*****','*****')

【讨论】:

    猜你喜欢
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-30
    • 2021-07-31
    • 2017-03-01
    • 2014-02-25
    相关资源
    最近更新 更多