[英]Python error FileNotFoundError: [Errno 2] No such file or directory:
Im trying to run my code with this but keep running into a file not found error.我试图用这个运行我的代码,但一直遇到文件未找到错误。
files = [i for i in os.listdir('C:/Users/me/Desktop/python data')]
for filename in files:
data = pandas.read_excel(str(filename))
I've tried looking around but cant seem to understand.我试过环顾四周,但似乎无法理解。
Running print(os.getcwd())
does find the file in the folder but i still get the error message运行
print(os.getcwd())
确实在文件夹中找到了文件,但我仍然收到错误消息
You need to concatenate the path and the filename returned from os.listdir
:您需要连接从
os.listdir
返回的路径和文件名:
PATH = 'C:/Users/me/Desktop/python data'
files = [os.path.join(PATH, i) for i in os.listdir(PATH)]
for filename in files:
data = pandas.read_excel(str(filename))
Further recommendations:进一步的建议:
pathlib
's .glob
to get the full path without using os.path.join
.pathlib
的.glob
来获取完整路径,而无需使用os.path.join
。read_excel
, please consider filtering by xls/xlsx files:read_excel
,请考虑按 xls/xlsx 文件过滤: Code example:代码示例:
import pathlib
path = pathlib.Path('C:/Users/me/Desktop/python data')
excel_filter = "*.xls*"
for filename in path.glob(excel_filter):
print(filename)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.