简体   繁体   中英

How to iterate through multiple excel files using python

I am trying to develop a python script that will iterate through several Excel .xlsx files, search each file for a set of values and save them to a new .xlsx template.

The issue I'm having is when I'm trying to get a proper list of files in the folder I'm looking at. I'm saving these filenames in a list variable 'fileList' to manage iteration.

When I run the code os.chdir(sourcepath) , I'm constantly getting a FileNotFoundError: [WinError 2] The system cannot find the file specified: C:\\\\Users\\\\username\\\\PycharmProjects\\\\projectName\\\\venv\\\\Site List\\\\siteListfolder

I think this has to do with the '\\\\' that is displaying in the error, but when I run a print(sourcepath) in this code, the path is properly displayed, with just one '\\' between each subdirectory instead of two.

I need to be able to get the list of files in the siteListfolder, and be able to iterate through them using this kind of logic:

priCLLI = sys.argv[1]
secCLLI = sys.argv[2]
sourcepath = os.path.join(homepath, 'Site List', f'{priCLLI}_{secCLLI}')
siteListfolder = os.listdir(sourcepath)
for file in siteListfolder:
   for row in file:
      <script does its work>

'siteListfolder = os.listdir(sourcepath)' is generating the error Thanks to all in advance for supporting this kind of forum.

import os

directory = ('your/path/directory')
Source_Workbook = []
for filename in os.listdir(directory):
    if filename.endswith(".xlsx"):
        Source_Workbook.append(filename)
        print(Source_Workbook)

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