简体   繁体   English

Python从单独文件夹中的jupyter笔记本访问excel文件

[英]Python accessing excel files from jupyter notebook in separate folder

As per the title i am having trouble accessing certain excel files in another folder based upon their filenames.根据标题,我无法根据文件名访问另一个文件夹中的某些 excel 文件。

I have a folder containing a bunch of excel files which share a common name,but all have an individual datestamp appended on as a suffix in the format /files/filename_%d%m%Y .xlsx giving me a directory like this:我有一个文件夹,其中包含一堆共享一个通用名称的 excel 文件,但所有文件都附加了一个单独的日期戳作为后缀,格式为/files/filename_%d%m%Y .xlsx 给我一个这样的目录:

├── files
│   ├── filename_10102021.xlsx
│   ├── filename_07102021.xlsx
│   ├── filename_11102021.xlsx
│   └── filename_14102021.xlsx
├── notebooks
│   └── my_notebook.ipynb

From the my_notebook.ipynb file, I would like to navigate to the files directory, and get the 2 most recent excel files according to the suffixed date and open them in the notebook as pandas dfs so I can compare the columns for any differences.my_notebook.ipynb文件中,我想导航到files目录,并根据后缀日期获取 2 个最新的 excel 文件,并将它们作为 pandas dfs 在笔记本中打开,以便我可以比较各列的任何差异。 In the directory I provided above, the 2 files I would get are filename_14102021.xlsx and filename_11102021.xlsx but would like this solution to work dynamically as the files folder gets new files with new dates as time goes on.在我上面提供的目录中,我将获得的 2 个文件是filename_14102021.xlsxfilename_11102021.xlsx但希望该解决方案能够动态工作,因为随着时间的推移,文件夹会获取具有新日期的新文件。 (so hardcoding those two names would not work) (所以硬编码这两个名字是行不通的)

My first thought is to do something like:我的第一个想法是做这样的事情:

import os
import sys
import pandas as pd

sys.path.append('../files')
files = sorted(os.listdir(), reverse=True)

most_recent_df = pd.read_excel(files[0], engine='openpyxl', index_col=0)  
second_most_recent_df = pd.read_excel(files[1], engine='openpyxl', index_col=0)  

and then do my comparison between the dataframes.然后在数据帧之间进行比较。

However this code fails to do what I want as even with using sys.path.append , the os.listdir function returns a list of the notebooks directory which tells me the problem lies in this 2 lines:但是,即使使用sys.path.append ,此代码也无法执行我想要的操作, os.listdir函数返回 notebooks 目录的列表,它告诉我问题出在os.listdir行中:

sys.path.append('../files')
files = sorted(os.listdir(), reverse=True)

How do I fix my code to move into the files directory so that a list of all the excel files is returned?如何修复我的代码以移动到 files 目录,以便返回所有 excel 文件的列表?

thank you!谢谢你!

It should work directly using它应该直接使用

files = sorted(os.listdir(r'path\to\folder'), reverse=True)

IMO you don't need to use sys.path.append海事组织你不需要使用sys.path.append

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM