Python Forum
Error when running mktorrent subprocess command
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error when running mktorrent subprocess command
#1
Hi there,

I can't figure out why my mktorrent subprocess command is failing. Any help in understanding why would be much appreciated.

Here's my code:
import os
import subprocess

target_file = "C:\\Users\\REMOVED\\python_apps\\my_progs\\test_folder\\test.test-test.mp4"
temp_dest = "C:\\Users\\REMOVED\\python_apps\\my_progs"
final_dest = os.path.join(temp_dest, "test_folder")
print(final_dest)
basename = os.path.basename(target_file)
torrent_name = basename + ".torrent"
print(torrent_name)
filesize = float(os.path.getsize(target_file))
#print(filesize)
filesize_gb = round(filesize / (1024 * 1024 * 1024), 3)

#print(filesize_gb)
if filesize_gb >= 16:
    piece_size = 24
elif filesize_gb >= 8 and filesize_gb < 16:
    piece_size = 23
elif filesize_gb >= 4 and filesize_gb < 8:
    piece_size = 22
elif filesize_gb >= 2 and filesize_gb < 4:
    piece_size = 21
elif filesize_gb >= 1 and filesize_gb < 2:
    piece_size = 20
elif filesize_gb >= 0.5 and filesize_gb < 1:
    piece_size = 19
elif filesize_gb >= 0.35 and filesize_gb < 0.5:
    piece_size = 18
elif filesize_gb >= 0.15 and filesize_gb < 0.35:
    piece_size = 17
elif filesize_gb >= 0.05 and filesize_gb < 0.15:
    piece_size = 16
elif filesize_gb <= 0.05:
    piece_size = 15
#print(piece_size)

os.chdir(final_dest)
subprocess.run('mktorrent', '-vp', '-s', 'HUH', '-l', piece_size, '-a', 'https://sometracker.org/announce.php', target_file, '-o', torrent_name, final_dest)
I'm receiving this error:
Error:
Traceback (most recent call last): File "C:\Users\REMOVED\python_apps\my_progs\create_torrent\create_torrent.py", line 39, in <module> subprocess.run('mktorrent', '-vp', '-s', 'HUH', '-l', piece_size, '-a', 'https://sometracker.org/announce.php', target_file, '-o', torrent_name, final_dest) File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 489, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 753, in __init__ raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
Here is the expected command and I tested in cmd with success.
mktorrent -vp -s HUH -l 16 -a http://sometracker.org/announce.php test.test-test.mp4 -o test.test-test.mp4.torrent C:\Users\REMOVED\python_apps\my_progs\test_folder
Reply
#2
The first argument to subprocess.run is the list of arguments. You're not passing them as a list. The second argument (optional) is bufsize. Your second argument of '-vp' isn't an integer, so it complains about it at that point.

#Incorrect
subprocess.run('/bin/ls', 'myfile')
#Correct
subprocess.run(['bin/ls', 'myfile'])

Also, you can't enter the elif sections unless you've failed the previous if/elifs. So for instance in the first elif, you already know that filesize must be < 16, so you don't have to check for it again.

# Equivalent to above:
if filesize_gb >= 16:
    piece_size = 24
elif filesize_gb >= 8:
    piece_size = 23
elif filesize_gb >= 4:
    piece_size = 22
elif filesize_gb >= 2:
    piece_size = 21
...
Reply
#3
Thanks for the response.

To address the first part, I should be able to just add the brackets around the list of arguments, right?
subprocess.run(['mktorrent', '-vp', '-s', 'HUH', '-l', piece_size, '-a', 'https://sometracker.org/announce.php', target_file, '-o', torrent_name, final_dest])
With that change, I'm now receiving the following error:
Error:
Traceback (most recent call last): File "C:\Users\REMOVED\python_apps\my_progs\create_torrent\create_torrent.py", line 39, in <module> subprocess.run(['mktorrent', '-vp', '-s', 'HUH', '-l', piece_size, '-a', 'https://sometracker.org/announce.php', target_file, '-o', torrent_name, final_dest]) File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 489, in run with Popen(*popenargs, **kwargs) as process: File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 1247, in _execute_child args = list2cmdline(args) File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\subprocess.py", line 549, in list2cmdline for arg in map(os.fsdecode, seq): File "C:\Users\REMOVED\AppData\Local\Programs\Python\Python38-32\lib\os.py", line 818, in fsdecode filename = fspath(filename) # Does type-checking of `filename`. TypeError: expected str, bytes or os.PathLike object, not int
The only integer in the command is piece_size. Does this mean that there's an issue with the command argument order? I was hoping to avoid that by verifying the expected command worked in cmd.

Your elif block suggestion is great. Thank you!
Reply
#4
(Sep-16-2020, 01:23 AM)pythonnewbie138 Wrote: The only integer in the command is piece_size. Does this mean that there's an issue with the command argument order?
I came to a different hypothesis. Here's my test:
Output:
>>> import subprocess >>> subprocess.run(['echo', '1']) 1 CompletedProcess(args=['echo', '1'], returncode=0) >>> subprocess.run(['echo', 1]) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/anaconda3/lib/python3.8/subprocess.py", line 489, in run with Popen(*popenargs, **kwargs) as process: File "/opt/anaconda3/lib/python3.8/subprocess.py", line 854, in __init__ self._execute_child(args, executable, preexec_fn, close_fds, File "/opt/anaconda3/lib/python3.8/subprocess.py", line 1637, in _execute_child self.pid = _posixsubprocess.fork_exec( TypeError: expected str, bytes or os.PathLike object, not int
Reply
#5
Got it. Thanks!

Simply changing piece_size to str(piece_size) fixed the problem. Definitely a facepalm moment haha.

Thanks again to both of you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  error "cannot identify image file" part way through running hatflyer 0 613 Nov-02-2023, 11:45 PM
Last Post: hatflyer
  error: invalid command 'egg_info' TimTu 0 907 Jul-27-2023, 07:30 AM
Last Post: TimTu
  Error when running kivy on python janeik 8 1,922 Jun-16-2023, 10:58 PM
Last Post: janeik
  Using subprocess to execute complex command with many arguments medatib531 5 1,718 Apr-27-2023, 02:23 PM
Last Post: medatib531
  Running script with subprocess in another directory paul18fr 1 3,476 Jan-20-2023, 02:33 PM
Last Post: paul18fr
  Command error - cursor.executemany(comandoSQL,valoresInserir) TecInfo 2 1,302 Nov-18-2022, 01:57 PM
Last Post: TecInfo
  Getting error when running "MINUS" between 2 databases marlonbown 4 1,217 Nov-10-2022, 05:49 AM
Last Post: deanhystad
  Error while running code on VSC maiya 4 3,544 Jul-01-2022, 02:51 PM
Last Post: maiya
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,321 Jun-18-2022, 01:09 PM
Last Post: snippsat
  Error when running a matplot lib example aurelius_nero 3 6,686 Apr-24-2022, 01:24 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020