基本的python IO,请了解文件目录的用法。

时间:2018-07-13 12:31:19

标签: python pandas io

这有效:

fpath = 'D:\\OSGM\\Data\\'
fname_in = 'OGSM_filtJul13.csv'
full_name_in = fpath + fname_in
dataframe = pd.read_csv(full_name_in)

但这不是:

Dat = pd.read_csv('D:\\OGSM\\Data\\OGSM_filtJul13.csv')

并返回: FileNotFoundError:文件b'D:\ OGSM \ Data \ OGSM_filtJul13.csv'不存在

很明显,这些是我关于IO文件/目录语法所缺少的。

有什么建议吗? P

1 个答案:

答案 0 :(得分:0)

所以问题似乎是您的字符串被强制为字节字符串。我重现了您的问题:

首先,用一个字节串

> pd.read_csv(b'test.csv')
Traceback (most recent call last):
  File "python", line 1, in <module>
  File "pandas/_libs/parsers.pyx", line 384, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas/_libs/parsers.pyx", line 713, in pandas._libs.parsers.TextReader._setup_parser_source
OSError: Expected file path name or file-like object, got <class 'bytes'> type

位置:

>  pd.read_csv('test.csv')
output:
=> Empty DataFrame
Columns: [foo;bar]
Index: []

>  pd.read_csv(u'test.csv')
=> Empty DataFrame
Columns: [foo;bar]
Index: []

为避免此类问题,尤其是在Windows 中, 尝试使用适当的os.path方法来连接路径:

import os

fpath = os.path.join("D:", os.sep, "OSGM", "Data")
fname_in = 'OGSM_filtJul13.csv'
full_name_in = os.path.join(fpath,fname_in)
dataframe = pd.read_csv(full_name_in)

N.B:在python2中,似乎没有发生此问题。