python - How to check the folder for file and then read the file in python
问题描述
I need to read a csv from a folder location, but there is catch in it. It could also happen my other module which writes the csv into that folder fails and unable to export the file, so basically i have to check the folder that if a csv file called "test.csv" exist or not in that folder, if it exist read the file else print('file not found')
folder name = file, file_name= test.csv
try:
df = pd.read_csv('filepath/file/test.csv') --- read if it is present in the folder
except error:
result = 'File not Found' --- catch message in a variable
解决方案
您可以使用 python 的 os 模块来检查文件是否存在。
以下是示例:
import os.path
os.path.isfile('./final_data.csv')
这将根据文件是否存在返回真或假。
推荐阅读
- c++ - 将基类中的受保护枚举类用作派生类中的公共枚举类的问题 [MSVC 2019]
- xcode - 单击“停止构建”时,Xcode 正在与正在运行的进程分离
- c# - 我收到一个错误 - 当前上下文中不存在名称“Vector2”
- java - 如何在条件查询中的非实体字段上创建 orderby?
- java - 根据 Java 中的父类中的元素(也按顺序)验证 2 个列表是否相等
- r - Shiny SidebarPanel selectInput 仅在选中复选框后
- graphdb - Java 兼容性 -- GraphDB 免费版 -- 独立变体
- javascript - 如何添加子值,检查我的代码以了解
- php - 预订完成后如何发送邮件
- android - Dagger2 注入抽象 ViewModel 的特定实例