Python-编写文件Errno 2的问题

时间:2019-03-09 14:46:07

标签: python

我是Python新手,我遇到的问题可能是一个简单的答案。我有以下脚本,该脚本在大多数情况下都有效,只是尝试写输出文件而卡住了。我收到的错误在最后:IOError:[Errno 2]没有这样的文件或目录:'/D/1_NEW_ANALYSIS/Scripts/Melodic_fsfs/design_Rat01_Run_1.fsf'

代码如下:

import os
import glob

studydir = 'D:/1_NEW_ANALYSIS'

fsfdir="%s/Scripts/Melodic_fsfs"%(studydir)
templatedir="%s/Scripts/Templates"%(studydir)

subdirs=glob.glob("%s/Subjects/Rat_[0-9][0-9]/Run_[0-2]"%(studydir))

for dir in list(subdirs):
  splitdir = dir.split('\\')
  # YOU WILL NEED TO EDIT THIS TO GRAB sub001
  splitdir_sub = splitdir[1]
  subnum=splitdir_sub[-2:]

  splitdir_run = splitdir[2]
  runnum=splitdir_run[-1:]
  print(subnum)

  replacements = {'SUBNUM':subnum, 'RUNNUM':runnum}
  with open("%s/Melodic_design.fsf"%(templatedir)) as infile: 
    with open("%s/design_Rat%s_Run_%s.fsf"%(fsfdir, subnum, runnum), 'w') as outfile:
        for line in infile: 
          for src, target in replacements.items():
            line = line.replace(src, target)
          outfile.write(line)

有人知道为什么它不起作用吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

如果您正在Windows上运行(我想是的),studydir应该如下所示:

studydir = 'D:\\1_NEW_ANALYSIS'