这里有新的python2.7用户。我搜索过类似的查询,但我看不出自己做错了什么。我有一个简短的脚本来读取目录中的所有文件,并依次读取每个文件,将它们写入单个主文件。
我的代码如下;我此时可以看到两个出错的地方(虽然我没有收到任何错误消息),1 - 它似乎只打开列表中的第一个文件,它应该是循环的(所以我的猜测是我使用了一个错误) glob?)和2 - 虽然print(str)语句显示给控制台nps,但输出文件也永远不会被写入。
我已经仔细检查过该文件是否存在,它是空的,我正在传递正确的路径&我调用函数时的文件名。
非常感谢任何帮助。
#!/usr/bin/env python
import glob
import sys
filestoberecognised=sys.argv[1]
outputfile=sys.argv[2]
filecontents=glob.glob(filestoberecognised)
with open(outputfile,'w+') as f:
for i, row in enumerate(filecontents):
print(row) # this correctly prints to console
f.write(row+'\n') # this should write the filename of the filestoberecognised to the outputfile
with open(row,'r') as labfile:
for j, line in enumerate(labfile): # this should write words in label file
f.write('%s'%(line))
print('%s'%(line))
labfile.close() # ensures each file looped through is closed
f.write('\n.\n')
f.flush()
f.close()