I am trying to read in a list of Excel files from a share drive in Windows using pandas
.
For example,
import pandas as pd
df = pd.DataFrame()
# new_files is a list of file paths to the Excel files on the share drive
for f in new_files:
tmp = pd.read_excel(f)
df = pd.concat([df, tmp], axis=0)
The file names in new_files
are generated by an automated process and have very long file paths (more than 259 characters).
When I try to read the files I get the below error.
Pandas Error: FileNotFoundError: [Err no 2] No such file or directory
At first I thought my share drive parent path was wrong, but it is fine,
import os
path_ = '\\\\acme.corp\\path\\to\\lots\\more\\sub\\folders'
os.path.exists(path_)
True
But the full file path does not exist (although I can see the file in the terminal),
os.path.exists(os.path.join(path_, 'very long filename.xlsx'))
False
When I opened it in Excel it seems this is actually a Windows/Excel problem,
Microsoft Excel Error: Cannot open the file because the file path is more than 259 characters. Try shortening the path or file name
If I copy the file to a local drive or shorter path then I can open it with pandas
and Excel which means the files are fine.
I was wondering if there is a way to read these Excel files in pandas
(or Python) with these very long file paths?
To avoid copying the files to another location as they are large.
I am running on Windows 10, Python 3.9.5, Pandas 1.3.1