python - 对文件夹进行计算
问题描述
我有一个主目录和 9 个子文件夹。我必须首先阅读路径并在每个文件夹中排除特定文件,并使用文件夹名称的图例绘制结果。问题是我可以看到我需要进行计算的文件,但它没有任何作用。我写的代码如下:
from __future__ import division
import sys
import os
import numpy as np
import matplotlib.pyplot as plt
import glob
import seaborn as sns
from scipy import stats
from scipy.stats.kde import gaussian_kde
mean_curv = []
FILES=[]
for r, d, f in os.walk(r'C:\Users\Hasan\Desktop\output\new our scenario\beta 15\test'):
for dirs in d:
CASES = [f for f in sorted(files) if f.startswith('config')]
maxnum = np.max([int(os.path.splitext(f)[0].split('_')[1]) for f in CASES])
CASES = ['configuration_%d.out' % i for i in range(maxnum)]
FILES.append(CASES)
for i, d in enumerate(FILES):
a = np.loadtxt(d).T
num = os.path.splitext(d)[0]
local_curv = np.abs(a[4])
mean_curv.append(np.mean(local_curv))
Time = np.arange(0,len(mean_curv))
plt.plot(Time,mean_curv)
我得到的错误如下:
ValueError Traceback (most recent call last)
<ipython-input-62-4e1e3e29813a> in <module>
1 for i, d in enumerate(RIVERS):
----> 2 a = np.loadtxt(d).T
3 num = os.path.splitext(d)[0]
4 local_curv = np.abs(a[4])
5 mean_curv.append(np.mean(local_curv))
~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows)
1157 # converting the data
1158 X = None
-> 1159 for x in read_data(_loadtxt_chunksize):
1160 if X is None:
1161 X = np.array(x, dtype)
~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in read_data(chunk_size)
1085
1086 # Convert each value according to its column and store
-> 1087 items = [conv(val) for (conv, val) in zip(converters, vals)]
1088
1089 # Then pack it according to the dtype's nesting
~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in <listcomp>(.0)
1085
1086 # Convert each value according to its column and store
-> 1087 items = [conv(val) for (conv, val) in zip(converters, vals)]
1088
1089 # Then pack it according to the dtype's nesting
~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in floatconv(x)
792 if '0x' in x:
793 return float.fromhex(x)
--> 794 return float(x)
795
796 typ = dtype.type
ValueError: could not convert string to float: 'configuration_0.out'
解决方案
您忽略了文件的路径,而不是:
a = np.loadtxt(d).T
你应该使用:
a = np.loadtxt(os.path.join(r, d)).T
推荐阅读
- c++ - 如何使用 Boost 的 vf2_subgraph_iso 检测多图上的子图同构?
- chart.js - 折线图绘制额外的 Y 值
- php - 为什么添加时我的锚标记不起作用?锚后
- javascript - 如何检查数组中是否有相等的值
- java - 这是否意味着 Java 不再免费了?
- javascript - 带有 API 的 Javascript 过滤表
- swift - 流套接字服务器端swift
- jquery - jquery中用于搜索框的隐藏对象
- ruby-on-rails - has_and_belongs_to_many 关联是否可以包含“所有者”?
- c# - MessageLockLostException 中断服务总线 QueueClient