Skip to content
Advertisement

How to read most recent file with Pandas? Output path is undefined?

I’m trying to read the two latest sheets in my folder READ1 and READ2 with pandas. Usually when I read files the file name has to be formatted at 'File.xlsx' but the method I’m using is printing in the terminal as File.xlsx. I tried changing the format with:

one = [str("'")+str(READ1)+ str("'")]
print(one)

Which outputs as ["'None'"]

My Code:

import glob
import os
import os.path
import pandas as pd
import xlsxwriter as xl
from pandas_datareader import data as pdr
import numpy as np


latest_file = sorted(glob.iglob('C:My Folder*'), key=os.path.getmtime) 
READ1 = print(latest_file[0])
READ2 = print(latest_file[1])


File1 = pd.read_excel(READ1,sheet_name='Sheet1', header=None)
File2 = pd.read_excel(READ2,sheet_name='Sheet1', header=None)
print(File1)

If I run my code as is I get

inspect_excel_format assert content_or_path is not None
AssertionError

I have tried changing them to csv files too but that doesn’t change anything. I think Python is reading it as an undefined variable. Such as:

READ1 = [File.xlsx]

has the error:

NameError: name 'File' is not defined

I have been referenceing: How to get the latest file in a folder? https://datatofish.com/latest-file-python/

Advertisement

Answer

The print method just prints its arguments to the terminal and returns None, so READ1 and READ2 are None.

Replace:

READ1 = print(latest_file[0])
READ2 = print(latest_file[1])

with:

READ1 = latest_file[0]
READ2 = latest_file[1]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement