简体   繁体   中英

how to get the name of an unknown .XLS file into a variable in Python 3.7

I'm using Python 3.7.

I have to download an excel file (.xls) that has a unique filename every time I download it into a specific downloads folder location.

Then with Python and Pandas, I then have to open the excel file and read/convert it to a dataframe.

I want to automate the process, but I'm having trouble telling Python to get the full name of the XLS file as a variable, which will then be used by pandas:

# add dependencies and set location for downloads folder

import os
import glob
import pandas as pd

download_dir = '/Users/Aaron/Downloads/'

# change working directory to download directory
os.chdir(download_dir)

# get filename of excel file to read into pandas
excel_files = glob.glob('*.xls')
blah = str(excel_files)
blah

So then for example, the output for "blah" is:

"['63676532355861.xls']"

I have also tried just using "blah = print(excel_files)" for the above block, instead of the "str" method, and assigning that to a variable, which still doesn't work.

And then the rest of the process would do the following:

# open excel (XLS) file with unknown filename in pandas as a dataframe
data_df = pd.read_excel('WHATEVER.xls', sheet_name=None)

And then after I convert it to a data frame, I want to DELETE the excel file.

So far, I have spent a lot of time reading about fnames, io, open, os.path, and other libraries.

I still don't know how to get the name of the unknown .XLS file into a variable, and then later deleting that file.

Any suggestions would be greatly appreciated.

Check this,

lst = os.listdir()
matching = [s for s in lst if '.xls' in s]

matching will have all list of excel files.

As you are having only one excel file, you can save in variable like file_name = matching[0]

This code finds an xls file in your specified path reads the xls file and deletes the file.If your directory contains more than 1 xls file,It reads the last one.You can perform whatever operation you want if you find more than one xls files.

import os

for filename in os.listdir(os.getcwd()):
   if filename.endswith(".xls"):
    print(filename)
    #do your operation
    data_df = pd.read_excel(filename, sheet_name=None)
    os.remove(filename)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM