-
os.listdir只返回目录中的文件名,不返回路径,pandas需要文件的路径(或相对路径),除非文件和代码在同一个目录。
- 您最好学习
pathlib 模块,它将路径视为具有方法的对象,而不是字符串。
-
pathlib 可能需要一些时间来适应,但所有用于提取路径特定部分的方法,例如用于文件扩展名的.suffix,或用于文件名的.stem,都值得。
import pandas as pd
from pathlib import Path
# create the path object and get the files with .glob
files = Path('/CADEC/original').glob('ARTHROTEC*.ann')
# create a list of dataframes, 1 dataframe for each file
df_list = [pd.read_csv(file, sep='\t', header=None) for file in files]
# alternatively, create a dict of dataframes with the filename as the key
df_dict = {file.stem: pd.read_csv(file, sep='\t', header=None) for file in files}
示例
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] on win32
import os
...: from pathlib import Path
...: os.listdir('e:/PythonProjects/stack_overflow/t-files')
Out[2]:
['.ipynb_checkpoints',
'03900169.txt',
'142233.0.txt',
'153431.2.txt',
'17371271.txt',
'274301.5.txt',
'42010316.txt',
'429237.7.txt',
'570651.4.txt',
'65500027.txt',
'688599.3.txt',
'740103.5.txt',
'742537.6.txt',
'87505504.txt',
'90950222.txt',
't1.txt',
't2.txt',
't3.txt']
list(Path('e:/PythonProjects/stack_overflow/t-files').glob('*'))
Out[3]:
[WindowsPath('e:/PythonProjects/stack_overflow/t-files/.ipynb_checkpoints'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/03900169.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/142233.0.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/153431.2.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/17371271.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/274301.5.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/42010316.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/429237.7.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/570651.4.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/65500027.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/688599.3.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/740103.5.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/742537.6.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/87505504.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/90950222.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/t1.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/t2.txt'),
WindowsPath('e:/PythonProjects/stack_overflow/t-files/t3.txt')]