【发布时间】:2020-09-17 00:20:29
【问题描述】:
如果我从某个链接下载具有特定扩展名的文件,但想下载具有其他扩展名的文件(例如 .doc 而不是 .bin),我将如何在 python 代码中执行此操作?
【问题讨论】:
标签: python download file-extension downloadfile
如果我从某个链接下载具有特定扩展名的文件,但想下载具有其他扩展名的文件(例如 .doc 而不是 .bin),我将如何在 python 代码中执行此操作?
【问题讨论】:
标签: python download file-extension downloadfile
可以通过以下方式完成:
这 3 个步骤都可以通过 Python 脚本自动完成。
https://pypi.org/project/pypandoc/
例如,将 markdown 文件转换为 rst-file(记得更正 URL):
import os
import requests
import pypandoc
# Download file
# TODO: Update URL
url = 'some_url/somefile.md'
r = requests.get(url)
orig_file = '/Users/user11508332/Downloads/somefile.md'
with open(orig_file, 'wb') as f:
f.write(r.content)
# pypandoc file extention conversion
output = pypandoc.convert_file(orig_file, 'rst')
# TODO: Place a check here to see if the new file got created
# Clean-up: Delete original file
# TODO: Place a check here to see if the old file still exists, in that case, proceed with deletion:
# os.remove(orig_file)
【讨论】: