Locating and reading all .py files

Solution for Locating and reading all .py files
is Given Below:

when i start the programm in vs every thing is working fine, but when i start it with python over the cmd there is no output

import glob
Liste = ""
progs = glob.glob("*/*.py")
for Prog in progs:
    fh = open(Prog, "r")
    pCode = fh.readlines()
    fh.close()
    for line in pCode:
        Liste +=(line)

so when i want to print the variable: “Liste” there is (when i am starting in vs) all the codes but when i am starting in cmd its blank, please help

I assume you don’t invoke your script from the same directory in Visual Studio than you do in the console…

glob.glob("*/*.py") creates a list of strings containing relative paths to Python files in any sub directory of the current working directory.

So if you invoke the script using different working directories will return different results.

See also:

It’s because you aren’t actually doing anything with Liste afterwards. You will see an output if you just print it at the end.

import glob
Liste = ""
progs = glob.glob("*/*.py")
for Prog in progs:
    fh = open(Prog, "r")
    pCode = fh.readlines()
    fh.close()
    for line in pCode:
        Liste +=(line)
print(Liste) 

Also, this would make it more readable.

import glob

liste = ""
progs = glob.glob("*/*.py")
for prog in progs:
    with open(prog, "r") as fh:
        p_code = fh.readlines()
    for line in p_code:
        liste +=(line)
print(liste)