Python 下载Torrent文件并使用urllib2将其保存在用户定义的目录中?
如何下载torrent文件并使用urllib 2将其保存在用户定义的目录中?这对于下载torrent文件并不重要。 您可以通过以下方式下载和保存任何类型的文件:Python 下载Torrent文件并使用urllib2将其保存在用户定义的目录中?,python,django,Python,Django,如何下载torrent文件并使用urllib 2将其保存在用户定义的目录中?这对于下载torrent文件并不重要。 您可以通过以下方式下载和保存任何类型的文件: import urllib2 with open("mytorrent", "w") as f: f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read()) 来自http://megatorrent.com/torrent-url将作为mytorren
import urllib2
with open("mytorrent", "w") as f:
f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read())
来自http://megatorrent.com/torrent-url
将作为mytorrent
保存在当前目录中
如果要将文件保存到其他目录中,请执行以下操作:
import urllib2
with open(os.path.join(torrents_die_path, "mytorrent"), "w") as f :
f.write(urllib2.urlopen('http://megatorrent.com/torrent-url').read())
我建议使用urllib2.urlopen而不是自己使用urllib2.urlopen来完成这项工作,因为这将在幕后完成这项工作。Python文档建议不要使用
file()
来打开文件-使用open()
和with
语句是最好的方法。事实上,它在3.x中被删除