运行下载链接,然后用一个存储变量的URL将python的文件夹解开到python的文件夹

发布于 2025-02-02 12:53:22 字数 781 浏览 2 评论 0原文

如何制作Python中的脚本,该脚本通过用户通过输入中的控制台输入的值替换在存储库的特定URL中,以便能够下载.zip并自动下载时(文件夹)是在输入并打开包装的地方创建的,例如:

给定两个随机的github url:

https://github.com/exercism/python/archive/refs/heads/bobahop-patch-1.zip-1.zip

https://github.com/exercism/exercism/python/archive/rechive/refs/refs/heads/bobahop-patch-patch-2.zip < /a>

用户可以通过控制台输入“ patch-1”或“ patch-2”,并且该值将在URL中替换,然后将执行链接,并将其包含的.zip下载到存储库中。同时,将创建一个带有任何名称的文件夹(例如,用户在控制台中输入的值),并且下载的.zip将移至该文件夹,一旦那里进行解压缩。

How could a script in python be made that, through a value entered by the user through the console in an input, is replaced in a specific url of a repository to be able to download a .zip and when it is downloaded automatically, a folder is created where it is entered and would unpack, for example:

Given two random github urls:

https://github.com/exercism/python/archive/refs/heads/bobahop-patch-1.zip

https://github.com/exercism/python/archive/refs/heads/bobahop-patch-2.zip

The user could enter "patch-1" or "patch-2" by console and this value would be replaced in the url and in turn the link would be executed and the .zip it contains would be downloaded to the repository. Simultaneously, a folder with any name would be created (the value entered by the user in the console, for example) and the downloaded .zip would be moved to that folder and once there it would be decompressed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱本泡沫多脆弱 2025-02-09 12:53:22

Python具有

  • 标准模块urllib从网页获取/下载数据urllib.request.urlretrieve()
  • 标准Moduel os创建文件夹os.makedirs()并移动/重命名文件os.rename()
  • 标准模块zipfile compress/uncompress .zip file file
import os
import urllib.request
import zipfile

#user_input = input("What to download: ")
user_input = 'patch-1'

pattern = 'https://github.com/exercism/python/archive/refs/heads/bobahop-{}.zip'
url = pattern.format(user_input)

filename = f"{user_input}.zip"

print('From:', url)
print('  To:', filename)

urllib.request.urlretrieve(url, filename)

# ---

destination_folder = user_input
print('Create folder:', destination_folder)

os.makedirs(destination_folder, exist_ok=True)

# ---

print('Uncompress')

zip_file = zipfile.ZipFile(filename)
zip_file.extractall(destination_folder)

# ---

print("Move .zip to folder")

old_name = filename
new_name = os.path.join(destination_folder, filename)
os.rename(old_name, new_name)

Python has

  • standard module urllib to get/download data from web page urllib.request.urlretrieve()
  • standard moduel os to create folder os.makedirs() and move/rename file os.rename()
  • standard module zipfile to compress/uncompress .zip file
import os
import urllib.request
import zipfile

#user_input = input("What to download: ")
user_input = 'patch-1'

pattern = 'https://github.com/exercism/python/archive/refs/heads/bobahop-{}.zip'
url = pattern.format(user_input)

filename = f"{user_input}.zip"

print('From:', url)
print('  To:', filename)

urllib.request.urlretrieve(url, filename)

# ---

destination_folder = user_input
print('Create folder:', destination_folder)

os.makedirs(destination_folder, exist_ok=True)

# ---

print('Uncompress')

zip_file = zipfile.ZipFile(filename)
zip_file.extractall(destination_folder)

# ---

print("Move .zip to folder")

old_name = filename
new_name = os.path.join(destination_folder, filename)
os.rename(old_name, new_name)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文