Directory listing difference between terminal and python listdir

When I list the contents of a directory in terminal, for example /usr/share/fonts, I see
ajohan-comfortaa, jomolhari-fonts, opensymbol, adobe-source-code-pro, julietaula-montserrat paktype-askh-basic, etc.
However, when I do os.listdir in Python, I see [‘dejavu’, ‘eosrei-emojione’, ‘gnu-free’, ‘google-crosextra-caladea’, ‘google-crosextra-carlito’, ‘liberation-fonts’].

Why is there a difference?

Is your python running in a container of some sort? If so, maybe you are getting the results from the container.

2 Likes

Hi @grath , welcome to the forum. Please do take a minute to go through the introductory posts in the #start-here category if you’ve not had a chance to do so yet.

Have you sorted the output to see if the lists are the same?

$ ls /usr/share/fonts
aajohan-comfortaa
adobe-source-code-pro
anka-coder
bitstream-vera-sans-fonts
cantarell
ccicons
dejavu-sans-fonts
dejavu-sans-mono-fonts
dejavu-serif-fonts
fontawesome
gdouros-symbola
gnu-free
google-carlito-fonts
google-droid-sans-fonts
google-noto
google-noto-cjk
google-noto-emoji
google-noto-vf
google-roboto-slab-fonts
ht-caladea-fonts
iwona
jomolhari-fonts
julietaula-montserrat
khmer-os-content-fonts
khmer-os-system-fonts
lato
levien-inconsolata
liberation-mono
liberation-sans
liberation-serif
lm
lm-math
lohit-assamese
lohit-bengali
lohit-devanagari
lohit-gujarati
lohit-kannada
lohit-odia
lohit-tamil
lohit-telugu
material-icons-fonts
mathjax
mnsymbol
open-sans
opensymbol
paktype-naskh-basic
pt-sans-fonts
sil-abyssinica-fonts
sil-mingzat
sil-nuosu
sil-padauk
smc-meera
stix-fonts
tex-gyre
tex-gyre-math
thai-scalable
twemoji
urw-base35

With Python:

$ ipython3
import os
f = os.listdir('/usr/share/fonts')
print(sorted(f))

['aajohan-comfortaa', 'adobe-source-code-pro', 'anka-coder', 'bitstream-vera-sans-fonts', 'cantarell', 'ccicons', 'dejavu-sans-fonts', 'dejavu-sans-mono-fonts', 'dejavu-serif-fonts', 'fontawesome', 'gdouros-symbola', 'gnu-free', 'google-carlito-fonts', 'google-droid-sans-fonts', 'google-noto', 'google-noto-cjk', 'google-noto-emoji', 'google-noto-vf', 'google-roboto-slab-fonts', 'ht-caladea-fonts', 'iwona', 'jomolhari-fonts', 'julietaula-montserrat', 'khmer-os-content-fonts', 'khmer-os-system-fonts', 'lato', 'levien-inconsolata', 'liberation-mono', 'liberation-sans', 'liberation-serif', 'lm', 'lm-math', 'lohit-assamese', 'lohit-bengali', 'lohit-devanagari', 'lohit-gujarati', 'lohit-kannada', 'lohit-odia', 'lohit-tamil', 'lohit-telugu', 'material-icons-fonts', 'mathjax', 'mnsymbol', 'open-sans', 'opensymbol', 'paktype-naskh-basic', 'pt-sans-fonts', 'sil-abyssinica-fonts', 'sil-mingzat', 'sil-nuosu', 'sil-padauk', 'smc-meera', 'stix-fonts', 'tex-gyre', 'tex-gyre-math', 'thai-scalable', 'twemoji', 'urw-base35']

Looks the same here.

2 Likes

I ran the command in Pycharm (which is where I need to use it). I just checked running in the terminal and I get the correct results, but Pycharm gives [‘dejavu’, ‘eosrei-emojione’, ‘gnu-free’, ‘google-crosextra-caladea’, ‘google-crosextra-carlito’, ‘liberation-fonts’].

Did you install Pycharm via a flatpak? If so, it is running in a container that is partially sandboxed from your system.

If it is a flatpak, you could try giving it rights to your filesystem. I haven’t done this but I believe the command would be something like this:

sudo flatpak override com.jetbrains.PyCharm-Community --filesystem=host

Fair warning that the command basically given pycharm access to your whole filesystem.

Alternatively, you could just install an rpm-based package instead of using the flatpak.

2 Likes

I removed the flatpak version and installed using snap. Now it works as expected. Thanks for your help.

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.