0

My problem is that I have so many torrent files that I need to attach. I have them all in a list.

torrent_list = ['file1.torrent', 'file2.torrent', etc.......]

Downloading torrents by file:

torrent_file = open('my-torrent-file.torrent', 'rb')
qb.download_from_file(torrent_file)

Downloading multiple torrents by using files:

I Am not able to get it work without writing it manually like it is shown in the example. I want to load them all from the list. can someone help with this?

torrent_file_list = [open('1.torrent', 'rb'), open('2.torrent', 'rb')]
qb.download_from_file(torrent_file_list)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
Amachi
  • 27
  • 5

2 Answers2

2

Try like this:

torrent_list = ['file1.torrent', 'file2.torrent']
for myFile in torrent_list:
  with open(myFile,'rb') as torr:
    qb.download_from_file(torr)
Wasif
  • 14,755
  • 3
  • 14
  • 34
  • so, this method worked but after some time it breaks. via this examplea it was able to load 116 torrent files to my torrent client then its broke. – Amachi Oct 05 '20 at 17:13
0

Use a list comprehension.

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)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 1
    when it comes to the function `open` you must use it in a context manager, so the file close at the end. If you don't close the file then it could break it. And in this example you are not closing the file – Jorge Morgado Oct 05 '20 at 16:54
  • 1
    I believe that the garbage collector will automatically close the files in this case. – Barmar Oct 05 '20 at 16:56
  • You are right. I search for it and seems to be as you say. It is explained [here](https://stackoverflow.com/questions/49512990/does-python-gc-close-files-too#:~:text=Taken%20from%20the%20docs%20(python%203.6)%3A&text=If%20you%20don%27t%20explicitly,clean-up%20at%20different%20times.) Sorry and thanks, I didn't know that. – Jorge Morgado Oct 05 '20 at 17:01
  • 1
    You're right that it's generally preferable to open and close explicitly or use a context manager. But for this it was simpler to do it this way. – Barmar Oct 05 '20 at 17:02
  • so it is still not working for me. Traceback (most recent call last): File "D:\torrents\src\torrenthooker.py", line 15, in qb.download_from_file([open(torrent_list, 'rb') for filename in torrent_list]) File "D:\torrents\src\torrenthooker.py", line 15, in qb.download_from_file([open(torrent_list, 'rb') for filename in torrent_list]) TypeError: expected str, bytes or os.PathLike object, not list – Amachi Oct 05 '20 at 17:07
  • Then the documentation is wrong, because it says that it allows a list of open files. – Barmar Oct 05 '20 at 17:10
  • This should work just like `torrent_file_list = [open('1.torrent', 'rb'), open('2.torrent', 'rb')]` that you showed in your question. – Barmar Oct 05 '20 at 17:13
  • yes it works, but i have thousand of files in my list. – Amachi Oct 05 '20 at 17:24
  • Why does the number of files in the list make a difference? Either it allows a list or it doesn't. If it works when you create the list by hand, it should also work when the list is created using a list comprehension. – Barmar Oct 05 '20 at 17:25