Python Forum

Full Version: subprocess check_output text cut off
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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.
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.
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
(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.
(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