简体   繁体   中英

Why does a variable work, but not a list for opening a workbook using xlrd in python?

I'm extremely new to python so I appreciate any help. I've been attacking this from every angle I can imagine for the last 4 days in hopes I could figure it out myself, but I'm stumped.

I'm trying to create a list of files from a specific directory with the extension .xlsx. Then I want to take those file names from the list and run some function, passing the discovered file name into the function, and iterating it with each file name. I've tried to do this in multiple ways, but for some reason I it won't accept the file names.

import os
import xlrd

mypath = "some path to files"
fileslist = []

def find_files():
    for file in os.listdir(mypath):
        if file.endswith(".xlsx")
            fileslist.append(file)

def other_function():    
    book = xlrd.open_workbook(fileslist)

I can print fileslist and show that it's populated with the correct info. I've done this in both the main part of the script and also within the other_function() area. I also tried testing out naming a variable fileslist with a valid file name in the directory, let's say "file1.xlsx" and that works. Once I input it into a list, even if the only entry in the list is "file1.xlsx" I get the following error

book = xlrd.open_workbook(fileslist)
  File "/Library/Python/2.7/site-packages/xlrd/__init__.py", line 110, in open_workbook
    filename = os.path.expanduser(filename)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 254, in expanduser
    if not path.startswith('~'):
AttributeError: 'list' object has no attribute 'startswith'

xlrd is looking for a file path to open. A list (of files) is 1) not a filepath, but multiple file paths and 2) not a string, which is the specific error you're getting. A string (which is a Python object) has a method .startswith which allows xlrd to check if the first part of the filepath (you're supposed to give open_workbook ) is a ~ or not. xlrd probably did this because that would affect where it looks for the file.

xlrd.open_workbook is essentially trying to double click on the filepath you send it, you are (essentially) trying to click on all of the files in your list at the same time, which might be possible if you could have X different computer mice with a hand for each, but isn't really possible the way computers are typically built.

If you want to make a dictionary of the different workbooks you have, but opened with xlrd you can use this:

xlrd_wbs = dict()
for my_file in filelist:
    xlrd_wbs[my_file] = xlrd.open_workbook(my_file)

and then access the different files with:

xlrd_wbs[whatever_file_path_you_used]

I would use a dictionary here because it allows you to access which file you want more reliably, if you just want a list though, you can do this:

xlrd_wbs = []
for my_file in filelist:
    xlrd_wbs.append(xlrd.open_workbook(my_file))

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