1

I have written a Python3 script to download a Drupal tarball. I do this using:

Drupal_latest_url = 'http://ftp.drupal.org/files/projects/drupal-8.0.0-beta11.tar.gz'
filename, headers = urllib.request.urlretrieve(Drupal_latest_url)
shutil.unpack_archive(filename, '/tmp', 'gztar')

My problem is that I then want to move the contents of the directory from the tarball (drupal-8.0.0-beta11/*) to a new folder.

How do I get the name of that directory, so how do I get "drupal-8.0.0-beta11" into a variable?

Freek
  • 1,097
  • 2
  • 12
  • 30

1 Answers1

0

Oh, I still have this as unanswered... I don't know what my Python skills were at the moment I asked this questions (not very high level I assume) but now I'd simply do:

Drupal_latest_url.split('/')[-1].strip('.tar.gz')

To get the answer.

Freek
  • 1,097
  • 2
  • 12
  • 30
  • 1
    This assumes that whoever archived this directory didn't rename the tarball, which might be reasonable if you archived it yourself, but is not always true – RyanQuey Aug 08 '20 at 05:27
  • @RyanQuey How would one handle different names in a tar.gz? I am running into this particular problem. – illan Dec 30 '21 at 18:58
  • @illan probably just query the folder name that was extracted, maybe using e.g., [shutil.listdir()](https://stackoverflow.com/a/3207973/6952495) or something to that effect. – RyanQuey Dec 31 '21 at 14:05