Python Forum
subprocess check_output text cut off
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
subprocess check_output text cut off
#1
Python 3.11.2 on LMDE6 (Debian 12)

Why is the text cut off at the end?

from subprocess import check_output
cmd = "apt search vlc"
founded_packages = check_output(cmd, shell = True, universal_newlines=True).splitlines()

for lines in founded_packages[-5:]:
    print(lines)
Output:
p vlc-plugin-video-output:i386 - Multimediaplayer und -streamer (Plugins zu p vlc-plugin-video-splitter - Multimediaplayer und -streamer (Plugins zu p vlc-plugin-video-splitter:i386 - Multimediaplayer und -streamer (Plugins zu p vlc-plugin-visualization - Multimediaplayer und -streamer (Plugins zu p vlc-plugin-visualization:i386 - Multimediaplayer und -streamer (Plugins zu
Output in terminal:

Output:
p vlc-plugin-video-output:i386 - Multimediaplayer und -streamer (Plugins zur Videoausgabe) p vlc-plugin-video-splitter - Multimediaplayer und -streamer (Plugins zur Video-Verteilung) p vlc-plugin-video-splitter:i386 - Multimediaplayer und -streamer (Plugins zur Video-Verteilung) p vlc-plugin-visualization - Multimediaplayer und -streamer (Plugins zur Visuallisierung) p vlc-plugin-visualization:i386 - Multimediaplayer und -streamer (Plugins zur Visuallisierung)
Reply
#2
For me apt search vlc returns a different output but aptitude search vlc prints an output resembling yours. By any chance, are you running Linux Mint with a Python wrapper for apt calling aptitude instead of apt as in this discussion ?

In that case, try to call aptitude search vlc directly to see if the lines are truncated in the terminal. I ran your script with aptitude instead of apt and it worked in Kubuntu 24.04.
You can also use /usr/bin/apt instead of apt to get the normal apt output if apt is shadowed by another script.
Axel_Erfurt likes this post
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thanks,
aptitude search vlc
This provides a better result in the script, without a cut end. Just like I need it.

/usr/bin/apt search vlc
Also works in the script without a cut end, but with a different kind of output.
Reply
#4
Just use python-apt which does not require calling apt.

#!/usr/bin/python3

try:
    import apt
except ImportError:
    print("Please use Python from the system in /usr/bin/python3")
    print(
        "If the module `apt` is still not found, then install the missing package with:"
    )
    print("apt install python3-apt")
    raise SystemExit(1)


cache = apt.Cache()


def find_packages(match):
    return (pkg for pkg in cache if match in pkg.name)


def find_installed_packages(match):
    return (pkg for pkg in find_packages(match) if pkg.is_installed)


if __name__ == "__main__":
    for pkg in find_installed_packages("linux"):
        print(pkg.name)
The Documentation: https://apt-team.pages.debian.net/python...ache.Cache
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
(Feb-20-2025, 08:28 AM)DeaD_EyE Wrote: Just use python-apt which does not require calling apt.
The problem I have with python-apt is that there is no user-friendly documentation. We sail in the fog.
« We can solve any problem by introducing an extra level of indirection »
Reply
#6
(Feb-20-2025, 08:35 AM)Gribouillis Wrote: The problem I have with python-apt is that there is no user-friendly documentation. We sail in the fog.

ChatGPT told me how to do it and yes, the documentation is not good. Maybe ChatGPT could improve the Docs :-D
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an FFMpeg pass to subprocess.PIPE to treat list as text file? haihal 2 992 Nov-21-2024, 11:48 PM
Last Post: haihal

Forum Jump:

User Panel Messages

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