我最近把一组几乎完全相同的程序从我的mac电脑移到了我学校的windows上,虽然路径看起来是一样的(或者它们的末端),但它们不会正常运行。在
import glob
import pylab
from pylab import *
def main():
outfnam = "igdata.csv"
fpout = open(outfnam, "w")
nrows = 0
nprocessed = 0
nbadread = 0
filenames = [s.split("/")[1] for s in glob.glob("c/Cmos6_*.IG")]
dirnames = "c an0 an1 an2 an3 an4".split()
for suffix in filenames:
nrows += 1
row = []
row.append(suffix)
for dirnam in dirnames:
fnam = dirnam+"/"+suffix
lines = [l.strip() for l in open(fnam).readlines()]
nprocessed += 1
if len(lines)<5:
nbadread += 1
print "warning: file %s contains only %d lines"%(fnam, len(lines))
tdate = "N/A"
irrad = dirnam
Ig_zeroVds_largeVgs = 0.0
else:
data = loadtxt(fnam, skiprows=5)
tdate = lines[0].split(":")[1].strip()
irrad = lines[3].split(":")[1].strip()
# pull out last column (column "-1") from second-to-last row
Ig_zeroVds_largeVgs = data[-2,-1]
row.append(irrad)
row.append("%.3e"%(Ig_zeroVds_largeVgs))
fpout.write(", ".join(row) + "\n")
print "wrote %d rows to %s"%(nrows, outfnam)
print "processed %d input files, of which %d had missing data"%( \
nprocessed, nbadread)`
这个程序在mac电脑上运行得很好,但对于windows我一直在追求:
^{pr2}$已将0行写入文件名 已处理0个输入文件,其中o缺少数据
在我的mac电脑上我要排144行。。。在
有人有什么建议吗?在
如果脚本没有引发任何错误,这段代码很可能返回一个空列表。在
视为环球网在Windows上使用正斜杠可以很好地工作,问题很可能是找不到文件,这很可能意味着你提供的字符串中有一个错误。确保
"c/Cmos6_*.IG"
中没有任何错误。在如果问题不是由这引起的,那么不幸的是,我不知道为什么会发生。在
而且,当我尝试时,文件名返回环球网在Windows上有反斜杠,所以应该用
"\\"
来拆分。在从我的脑子里想,这似乎是一个使用/在路径中的问题。Windows使用\代替。在
os.path
包含许多函数,以便于跨平台使用路径。在你的
s.split("/")
应该是s.split(os.pathsep)
。我被这个咬过,有一次…:)事实上,
glob
在Windows上返回的路径是\
,在Mac OS X上是/
,因此您需要使用适当的路径分隔符(os.pathsep
)进行拆分。在相关问题 更多 >
编程相关推荐