using os.walk cannot open the file from the list

我的未来我决定 提交于 2019-12-02 17:53:09

问题


My problem is to read '.csv' files in catalogs and do some calculations on them. I have calculations working but my for loop seem not to work as I want to.

d = 'F:\MArcin\Experiments\csvCollection\'
for dirname, dirs, files in os.walk(d):

    for i in files:
        if i.endswith('.csv'):
            data1 = pd.read_csv(i, sep=",")
            data = data1['x'][:, np.newaxis]
            target = data1['y']

The error Iam getting is: IOError: File 1.csv does not exist

files is list of all '.csv' files inside dirname

i is str of size 1 and contains 1.csv (that is first of the files in catalog)

Any ideas why this is not working?

Thanks for any help.


回答1:


Because 1.csv is somewhere on the filesystem and when you call read_csv() it opens file relative to current directory.

Just open it using absolute path:

data1 = pd.read_csv(os.path.join(dirname, i), sep=",")

dirname in os.walk represents actual directory where file 1.csv is located.



来源:https://stackoverflow.com/questions/28932726/using-os-walk-cannot-open-the-file-from-the-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!