Python 从列表中添加多个文件时出现问题

Python 从列表中添加多个文件时出现问题,python,Python,我的问题是我有太多的torrent文件需要附加。我把它们都列在一张单子上 torrent_list = ['file1.torrent', 'file2.torrent', etc.......] 按文件下载torrents: torrent_file = open('my-torrent-file.torrent', 'rb') qb.download_from_file(torrent_file) 使用以下文件下载多个Torrent: 如果不像示例中所示那样手动编写它,我就无法让它工作。

我的问题是我有太多的torrent文件需要附加。我把它们都列在一张单子上

torrent_list = ['file1.torrent', 'file2.torrent', etc.......]
按文件下载torrents:

torrent_file = open('my-torrent-file.torrent', 'rb')
qb.download_from_file(torrent_file)
使用以下文件下载多个Torrent:

如果不像示例中所示那样手动编写它,我就无法让它工作。 我想从列表中全部加载它们。有人能帮忙吗

torrent_file_list = [open('1.torrent', 'rb'), open('2.torrent', 'rb')]
qb.download_from_file(torrent_file_list)

使用列表理解

torrent_file_list = [open(filename, 'rb') for filename in torrent_list])
qb.download_from_file(torrent_file)
for f in torrent_file_list:
    close(f)

使用列表理解

torrent_file_list = [open(filename, 'rb') for filename in torrent_list])
qb.download_from_file(torrent_file)
for f in torrent_file_list:
    close(f)
试着这样做:

torrent_list = ['file1.torrent', 'file2.torrent']
for myFile in torrent_list:
  with open(myFile,'rb') as torr:
    qb.download_from_file(torr)
试着这样做:

torrent_list = ['file1.torrent', 'file2.torrent']
for myFile in torrent_list:
  with open(myFile,'rb') as torr:
    qb.download_from_file(torr)

当涉及到函数
open
时,您必须在上下文管理器中使用它,以便文件在末尾关闭。如果你不关闭文件,它可能会破坏它。在这个例子中,你没有关闭文件,我相信在这种情况下,垃圾收集器会自动关闭文件,你是对的。我在寻找它,就像你说的那样。很抱歉,您是对的,通常最好显式打开和关闭或使用上下文管理器。但是这样做比较简单,所以对我来说仍然不起作用。回溯(最后一次调用):文件“D:\torrents\src\torrenthooker.py”,第15行,在qb中。从文件下载([open(torrent_列表,'rb')用于torrent_列表中的文件名])文件“D:\torrents\src\torrenthooker.py”,第15行,在qb中。从_文件下载([open(torrent_列表,'rb')'),用于torrent__列表中的文件名)类型错误:应为str,字节或os.PathLike对象,而不是列表当涉及函数
open
时,必须在上下文管理器中使用它,因此文件将在末尾关闭。如果你不关闭文件,它可能会破坏它。在这个例子中,你没有关闭文件,我相信在这种情况下,垃圾收集器会自动关闭文件,你是对的。我在寻找它,就像你说的那样。很抱歉,您是对的,通常最好显式打开和关闭或使用上下文管理器。但是这样做比较简单,所以对我来说仍然不起作用。回溯(最后一次调用):文件“D:\torrents\src\torrenthooker.py”,第15行,在qb中。从文件下载([open(torrent_列表,'rb')用于torrent_列表中的文件名])文件“D:\torrents\src\torrenthooker.py”,第15行,在qb中。从_文件下载([open(torrent_列表,'rb')'),用于torrent__列表中的文件名)类型错误:应为str,bytes或os.PathLike对象,而不是listso,此方法有效,但一段时间后会中断。通过这个例子,它能够加载116个torrent文件到我的torrent客户端,然后它就坏了。所以,这个方法工作了,但过了一段时间它就坏了。通过这个例子,它能够加载116个torrent文件到我的torrent客户端,然后它就坏了。