[英]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.我正在尝试开发一个 python 脚本,该脚本将遍历多个 Excel .xlsx 文件,在每个文件中搜索一组值并将它们保存到新的 .xlsx 模板中。
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.
我将这些文件名保存在列表变量“fileList”中以管理迭代。
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当我运行代码
os.chdir(sourcepath)
,我不断收到 FileNotFoundError: [WinError 2] The system cannot find the file specified: C:\\\\Users\\\\username\\\\PycharmProjects\\\\projectName\\\\venv\\ \\站点列表\\\\站点列表文件夹
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.我认为这与错误中显示的“\\\\”有关,但是当我在此代码中运行
print(sourcepath)
时,路径会正确显示,每个子目录之间只有一个“\\”而不是两个.
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:我需要能够获取 siteListfolder 中的文件列表,并能够使用这种逻辑遍历它们:
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. 'siteListfolder = os.listdir(sourcepath)' 产生错误提前感谢大家支持这种论坛。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.