Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下载Torrent文件并使用urllib2将其保存在用户定义的目录中?_Python_Django - Fatal编程技术网

Python 下载Torrent文件并使用urllib2将其保存在用户定义的目录中?

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

如何下载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
将作为
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中被删除