我们准备了以下python脚本(python 2.7)来制作直方图。
histogram.py
#!/usr/bin/env python
import sys
import numpy as np
import matplotlib as mpl
import matplotlib.mlab as mlab
mpl.use('Agg')
import matplotlib.pyplot as plt
sys.argv[1] # Define input name
sys.argv[2] # Define output name
sys.argv[3] # Define title
# Open the file name called "input_file"
input_file=sys.argv[1]
inp = open (input_file,"r")
lines = inp.readlines()
if len(lines) >= 20:
x = []
#numpoints = []
for line in lines:
# if int(line) > -10000: # Activate this line if you would like to filter any date (filter out values smaller than -10000 here)
x.append(float(line))
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=False, facecolor='gray')
plt.xlabel('Differences')
numpoints = len(lines)
plt.ylabel('Frequency ( n =' + str(numpoints) + ' ) ' )
title=sys.argv[3]
plt.title(title)
plt.grid(True)
save_file=sys.argv[2]
plt.savefig(save_file+".png")
plt.clf()
inp.close()
示例:输入
1
2
3
该脚本将执行以下操作
python histogram.py input ${output_file_name}.png ${title_name}
我们添加一行“if len(lines)> = 20:”所以如果数据点小于20,我们就不会制作情节。
但是,如果文件为空,则此python脚本将被冻结。
在运行“python histogram.py input $ {output_file_name} .png $ {title_name}”
之前,我们添加一个bash行来删除所有空文件find . -size 0 -delete
由于某些原因,这条线总是适用于小规模测试,但不能在几个循环下的实际生产中运行。因此,如果可能,我们希望使“histogram.py”忽略任何空文件。
搜索只找到这个似乎没有用的链接:(
Ignoring empty files from coverage report
有人可以提供一些意见吗?谢谢!
答案 0 :(得分:2)
检查input_file
文件是否为空os.path.getsize(input_file) > 0
您将需要我认为您将拥有的完整路径,如果该文件不存在或无法访问,则会引发错误,因此您可能需要处理这些情况。
此代码有效,忽略空文件:
#!/usr/bin/env python
import sys
import numpy as np
import matplotlib as mpl
import matplotlib.mlab as mlab
import os
mpl.use('Agg')
import matplotlib.pyplot as plt
sys.argv[1] # Define input name
sys.argv[2] # Define output name
sys.argv[3] # Define title
input_file=sys.argv[1]
# Open the file name called "input_file"
if os.path.getsize(input_file) > 0:
inp = open (input_file,"r")
lines = inp.readlines()
if len(lines) >= 20:
x = []
#numpoints = []
for line in lines:
# if int(line) > -10000: # Activate this line if you would like to filter any date (filter out values smaller than -10000 here)
x.append(float(line))
# the histogram of the data
n, bins, patches = plt.hist(x, 50, normed=False, facecolor='gray')
plt.xlabel('Differences')
numpoints = len(lines)
plt.ylabel('Frequency ( n =' + str(numpoints) + ' ) ' )
title=sys.argv[3]
plt.title(title)
plt.grid(True)
save_file=sys.argv[2]
plt.savefig(save_file+".png")
plt.clf()
inp.close()
else:
print "Empty file"
~$ python test.py empty.txt foo bar
Empty file
答案 1 :(得分:-1)
检查文件是否存在+之前是否为空。
import os
def emptyfile(filepath):
return ((os.path.isfile(filepath) > 0) and (os.path.getsize(filepath) > 0))