'Why doesn't my program give output or even error in python

Didn't give any output what can I do?

for files in os.listdir(dir):

    if files[-4::] == 'xlsx':
        file_1 = pd.ExcelFile((os.path.join(dir,files)),engine='openpyxl')
        print('Path of File: ', os.path.join(dir,files))
        print('Student Number: ', pd.read_excel(file_1, sheet_name=0).iloc[0,1])
        for names in sheets_names:
            sheet = file_1.sheet_names.index(names)
            print('Sheet: ', file_1.sheet_names[sheet])
            file_original = pd.read_excel(file_1, sheet_name=sheet,engine='openpyxl',header=0,usecols=None)
            df=file_original.copy()
            print(df)


Solution 1:[1]

Extending Ssayan's comment a bit

I can see two possible reasons you don't get any output

  1. there are no files in dir
  2. If there are files in dir: there are no files fulfilling your if statement

In both cases nothing happens that would produce an output.

I recommend to print os.listdir(dir) and see what files there are. Also print dir to see if the path is correct. Also verify that your file has the correct extension (and not eg xls or ods).

If you want error messages you have to add them by yourself. You could do it as follows:

file_found = False
for files in os.listdir(dir):
    print(f"processing file: '{files}'")
    if files[-4::] == 'xlsx':
        file_found = True
    else:
        print(f"current file does not end with xlsx. Its last 4 chars are: '{files[-4:]}'")
if not file_found:
    print("ERROR: There were no files with ending xlsx")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Markus