基本的python IO,请理解文件目录的使用方法。 [英] Basic python IO, please melp understanding file directory usage.

查看:626
本文介绍了基本的python 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:\ tOSM \ data \OGSM_filtJul13.csv'不存在

And returns: FileNotFoundError: File b'D:\OGSM\Data\OGSM_filtJul13.csv' does not exist

显然这些是我对IO缺少的文件/目录语法。

Clearly these is something I am missing about IO file/directory syntax.

有什么建议吗?
P

Any advice? P

推荐答案

所以问题似乎是你的字符串被强制转换为bytestring。我重新创建了你的问题:

So the problem seems that your string is coerced to bytestring. I recreated your problem:

首先,使用bytestring

First, with a bytestring

> 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方法来连接路径:

To avoid this kind of problems, especially in windows, Try using proper os.path methods to join paths :

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)

注意:在python2中,问题似乎不会发生。

N.B: In python2, the problem doesn't seem to occur.

这篇关于基本的python IO,请理解文件目录的使用方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆