简体   繁体   English

用os.walk()搜索所有excel文件,然后调用函数以读取此excel,我该怎么办?

[英]seaching all excel files with os.walk (), then call the function to read this excel, How can I do it?

I created a function to read excel sheet read_Excel_file(path) that return a list contains some data from a specific Column. 我创建了一个读取Excel工作表read_Excel_file(path)的函数,该函数返回一个列表,其中包含来自特定列的一些数据。

and in the main code I search all the excel files (where the name start Design) and this excel file should be saved in a folder Design . 然后在主代码中搜索所有excel文件(名称以Design开头),并且此excel文件应保存在Design文件夹中。 If I find the excel file, I call the function read_Excel_file . 如果找到excel文件,则调用函数read_Excel_file

Please find below the code: 请在下面的代码中找到:

import openpyxl as opx
import os


for r, d, f in os.walk('.'):
    for file in f:
        if '.xlsx'  and 'design' in file:
            #print(r)
            if r.endswith('\Design'): 
                print(file)                     
                read_Excel_file(file)                

but I get the error : 但我得到了错误:

No such file or directory 无此文件或目录

even if I am sure that I have this file in my directory 即使我确定我的目录中有此文件

Do you think that I have path problem? 你认为我有路径问题吗?

PS: I add print(file) just to check the name of the file, but when read_Excel_file(file) after that I have the error. PS:我添加print(file)只是为了检查print(file)的名称,但是当read_Excel_file(file)之后的read_Excel_file(file)出现错误时。

Can you help me please? 你能帮我吗?

File is just the name of the file. 文件只是文件名。 You are missing the complete adress. 您缺少完整的地址。 You need to add the root part of the address. 您需要添加地址的根部分。

Just do: 做就是了:

filepath = os.path.join(r, file)
read_Excel_file(filepath) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除--- &gt;&gt;之后的读取文件(对于root,dirs,os.walk()中的文件)&lt;&lt; - 在python中使用函数os.remove() - How can I delete the Read files after --->>(for root, dirs, files in os.walk()) <<— with function os.remove() in python os.walk onerror的函数返回到哪里?如何为onerror函数调用添加更多参数? - Where does the function for os.walk onerror return to? How do I add more arguments to the onerror function call? 如何在os.walk函数中过滤具有特定路径的文件? - How to filter the files with a certain path in os.walk function? 如何使用os.walk从子目录中的文件访问信息? - how do I access information from files in subdirectories with os.walk? 如何使用os.walk访问特定文件夹中特定文件中的信息 - how do I access information from specific files in specific folders with os.walk 如何将os.walk索引到数据框中? - How do I index os.walk into a dataframe? 如何在python中使用os.walk更改根文件夹和所有子目录中的ext? - How do i use os.walk in python to change ext within root folder and all subdirectories? 如何在Python中并行运行os.walk? - How do I run os.walk in parallel in Python? 如何使用 os.walk 找到文件的路径? - How do I find the path of a file using os.walk? 如何使用os.walk()重命名文件? - How to rename files using os.walk()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM