2011-10-29 21 views
1

我有一个短脚本的问题,如果你能看一看就会很棒!在不同的文件中使用子进程python

import os 
import subprocess 

root = "/Users/software/fmtomov1.0/remaker_lastplot/source_relocation/observed_arrivals_loc3d" 

def loop_loc3d(file_in): 
    """Loops loc3d over the source files""" 
    return subprocess.call (['loc3d'], shell=True) 

def relocation(): 
    for subdir, dirs, files in os.walk(root): 
     for file in files: 
      file_in = open(os.path.join(subdir, file), 'r') 
      return loop_loc3d(file_in) 

我觉得这个脚本很容易理解,它很简单。但是我没有得到想要的结果。我只需要'loc3d'来操作'observed_arrivals_loc3d'目录中存在的所有文件内容,这意味着我需要打开所有文件,这就是我实际完成的工作。事实上,如果我尝试在'打印文件'之后:

for subdir, dirs, files in os.walk(root) 

我会得到每个文件的名称。此外,如果我尝试后

file_in = open(os.path.join(subdir, file), 'r') 

一个“打印file_in”我得到这样的线,用于对文件:

<open file '/Users/software/fmtomov1.0/remaker_lastplot/source_relocation/observed_arrivals_loc3d/EVENT2580', mode 'r' at 0x78fe38> 

子已上只有一个文件单独测试,它的工作。

总的来说,我没有收到任何错误,只是-11,这对我来说毫无意义。 loc3d的输出应该完全不同。

那么代码对你来说看起来不错吗?有什么我失踪?任何建议?

感谢您的帮助!

+0

也可以考虑使用['make',由@Kirill Teplinskiy在评论中建议](http:// stackoverflow。COM/Q /4279分之7938128)。 – jfs

+0

这对我来说很难理解。无论如何,我尝试了你下面说的,没有任何反应(没有输出没有错误)。在这个阶段,我认为这是一个'loc3d'错误,因此就python而言,可能一切都很好。将尝试深入了解loc3d。 – eikonal

回答

0

刚刚发现loc3d,作为unutbu说,依赖于几个变量,并在特定的情况下,一个名为“observal_arrivals”我有每次从我的目录创建和删除。在Pythonic术语中,这意味着:

import os 
import shutil 
import subprocess 

def loop_loc3d(file_in): 
    """Loops loc3d over the source files""" 
    return subprocess.call(["loc3d"], shell=True) 

path = "/Users/software/fmtomo/remaker_lastplot/source_relocation" 
path2 = "/Users/Programming/working_directory/2test" 
new_file_name = 'observed_arrivals' 
def define_object_file(): 
    for filename in os.listdir("."): 
     file_in = os.rename (filename, new_file_name) # get the observal_arrivals file 
     file_in = shutil.copy ("/Users/simone/Programming/working_directory/2test/observed_arrivals", "/Users/software/fmtomo/remaker_lastplot/source_relocation") 
     os.chdir(path) # goes where loc3d is 
     loop_loc3d (file_in) 
     os.remove("/Users/software/fmtomo/remaker_lastplot/source_relocation/observed_arrivals") 
     os.remove ("/Users/Programming/working_directory/2test/observed_arrivals") 
     os.chdir(path2) 

现在,这是工作得很好,所以它应该回答我的问题。我想这很容易理解,它只是复制,改变目录和那种东西。

+0

1.不要将问题发布为答案。这个代码有很多错误。阅读[Python教程](http://docs.python.org/tut)。它会为你节省时间。 3. open('fort.14','r')'表示:打开文件fort'.14''供阅读。如果文件不存在,它不会创建它。 – jfs

+0

在这种情况下应该怎么做?我的意思是我不认为提出一个新问题是一个好主意,我不认为我可以在评论中发表。 fort14显然已经存在。 – eikonal

+0

创建一个*完整的*(即,您可以按原样运行),*最小*(即,如果您从中删除任何东西,问题就消失了)示例显示您的问题并将其作为新问题发布。如果你认为它是相关的,你可以添加一个链接到你以前的问题。 – jfs

1

我假设你会从CLI调用loc3d filename。如果是这样,那么:

def loop_loc3d(filename): 
    """Loops loc3d over the source files""" 
    return subprocess.call (['loc3d',filename]) 

def relocation(): 
    for subdir, dirs, files in os.walk(root): 
     for file in files: 
      filename = os.path.join(subdir, file) 
      return loop_loc3d(filename) 

换句话说,不要自己打开的文件,让loc3d做到这一点。

+0

..... .....同样的结果,-11 :( – eikonal

+0

locus文件名是否按照预期从命令行工作?loc3d是否有'-v'详细模式?如果是,调用'subprocess.call( ['loc3d',' - v',filename])'可能会给你更多的调试信息。 – unutbu

+0

是的,它可以工作....与-v它仍然工作,但现在不给任何输出,也没有错误。 – eikonal

1

当前您的重定位方法将在第一次迭代后返回(对于第一个文件)。你根本不需要返回。

def loop_loc3d(filename): 
    """Loops loc3d over the source files""" 
    return subprocess.call (['loc3d',filename]) 

def relocation(): 
    for subdir, dirs, files in os.walk(root): 
     for file in files: 
      filename = os.path.join(subdir, file) 
      loop_loc3d(filename) 

这只是其中一个问题。另一个涉及loc3d本身。尝试提供loc3d的完整路径。

+0

它看起来像是工作了几秒钟,但然后没有输出,没有错误.... – eikonal

+0

任何想法,我可以下载loc3d来测试它? –

+0

不幸的是,你不能,我想我不能附上任何东西,否则这将是一个好主意! – eikonal

0

-11退出代码可能意味着该命令被信号分段故障所杀死。 这是loc3d中的一个错误。行为良好的程序不应在任何用户输入中产生“分段错误”。

饲料loc3d只有它可以理解的文件。打印的文件名或使用subprocess.check_call()找出哪些文件,它不喜欢:

#!/usr/bin/env python 
import fnmatch 
import os 
import subprocess 

def loc3d_files(root): 
    for dirpath, dirs, files in os.walk(root, topdown=True): 
     # skip hidden directories 
     dirs[:] = [d for d in dirs if not d.startswith('.')] 
     # process only known files 
     for file in fnmatch.filter(files, "*some?pattern[0-9][0-9].[ch]"): 
      yield os.path.join(dirpath, file) 

for path in loc3d_files(root): 
    print path 
    subprocess.check_call(['loc3d', path]) # raise on any error 
+0

非常感谢J.F.,你真的接近这个问题,我的意思是,我经常会遇到'分段错误'。我会尽快尝试,我会告诉你。 – eikonal

+0

@eikonal:在评论中你说没有任何反应。如果你正在谈论上面的代码,那么你必须改变文件模式'“* some?pattern [0-9] [0-9]。[ch]”'例如,将所有.txt文件提供给'loc3d'你可以使用'“* .txt”'文件模式。如果您想从名称以点开头的目录处理文件,那么您必须删除上面代码中的'dir [:] = ...'行。如果'loc3d_files()'产生至少一个文件名,你至少应该在输出中看到一些东西。 – jfs

+0

请参阅上面的帖子... – eikonal

相关问题