如果我要从某个链接下载具有某个扩展名的文件,但又想下载具有另一个扩展名的文件(例如,.doc而不是.bin),我将如何在python代码中执行此操作? / p>
答案 0 :(得分:0)
可以通过以下方式完成:
这3个步骤都可以通过Python脚本自动执行。
https://pypi.org/project/pypandoc/
例如,将markdown文件转换为rst文件(请记住以更正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)