1

Morning Everybody,

I've a problem concerning my code. I'll explain, I've a connexion to a local FTP server on my linux instance. I would like to explore all my directories to unrar my files ".rar" but when I'm launching my script, there is just the display of directories that is working.

I don't know why.

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("")

Here the result:

[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

Thank you for your help

3
  • "if directories not in ['..', '.'];" Doesn't make sense in context.
    – JeffUK
    Feb 13, 2019 at 11:02
  • Yes, it was just a debug line for test. I did this line in comment Feb 13, 2019 at 11:04
  • Are you trying to use os.walk on the ftp side? Not sure if that is that easy; have a look here, though Feb 13, 2019 at 15:35

1 Answer 1

1

Thank you for you help, finally I've found the solution to make my script work.

Here my updated

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('*****','*****')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.