2014-07-07 117 views
1

下面的代码是我正在编写的程序的一部分,它在每个.py,.sh上运行一个方法。或.pl文件在目录及其文件夹中。Python IOError:[递归目录2]来自递归目录调用

for root, subs, files in os.walk("."): 
    for a in files: 
     if a.endswith('.py') or a.endswith('.sh') or a.endswith('.pl'): 
      scriptFile = open(a, 'r') 
      writer(writeFile, scriptFile) 
      scriptFile.close() 
     else: 
      continue 

在编写程序时,它曾在目录树中我写的,但是当我移动到另一个文件夹试试那里,我得到这个错误信息:

Traceback (most recent call last): 
File "versionTEST.py", line 75, in <module> 
scriptFile = open(a, 'r') 
IOError: [Errno 2] No such file or directory: 'enabledLogSources.sh' 

我知道奇怪的事情是怎么回事,因为该文件是最肯定有...

+0

是你的测试目录中的多个层次深?启用LogSources.sh从您运行此脚本的目录下层? – Hoopdady

+0

'open(os.path.join(“{}/{}”。format(root,a)),“r”)' –

+0

'writeFile'从哪里来? –

回答

1

你需要预先设置的根目录到您的文件名

scriptFile = open(root + '/' + a, 'r') 
+0

我用这种方法,它工作完美。谢谢! – Zach