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:
JavaScript
x
3
1
one = [str("'")+str(READ1)+ str("'")]
2
print(one)
3
Which outputs as ["'None'"]
My Code:
JavaScript
1
19
19
1
import glob
2
import os
3
import os.path
4
import pandas as pd
5
import xlsxwriter as xl
6
from pandas_datareader import data as pdr
7
import numpy as np
8
9
10
latest_file = sorted(glob.iglob('C:My Folder*'), key=os.path.getmtime)
11
READ1 = print(latest_file[0])
12
READ2 = print(latest_file[1])
13
14
15
File1 = pd.read_excel(READ1,sheet_name='Sheet1', header=None)
16
File2 = pd.read_excel(READ2,sheet_name='Sheet1', header=None)
17
print(File1)
18
19
If I run my code as is I get
JavaScript
1
3
1
inspect_excel_format assert content_or_path is not None
2
AssertionError
3
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:
JavaScript
1
2
1
READ1 = [File.xlsx]
2
has the error:
JavaScript
1
2
1
NameError: name 'File' is not defined
2
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:
JavaScript
1
3
1
READ1 = print(latest_file[0])
2
READ2 = print(latest_file[1])
3
with:
JavaScript
1
3
1
READ1 = latest_file[0]
2
READ2 = latest_file[1]
3