2013-08-28 94 views
0

我想将这个python代码转换成另一种格式的文件,它需要三个参数输入文件,输出文件和百分比值。我在shell脚本中调用了python函数,并且它第一次运行正常,但第二次不起作用。错误是“IOError:[Errno 2]没有这样的文件或目录:'ux_source_2850.txt'。但我很确定这个文件在这个目录中。有人能帮助我吗? 此外,我想知道是否有另一种方式来调用python函数,就像一个编译的c函数,所以我可以与几个参数一起执行函数。IOError:[Errno 2]没有这样的文件或目录:'ux_source_2850.txt

#!/usr/bin/env python 
def convertfile(file1,file2,percentage): 
    with open(file1, "r+") as infile, open(file2, "w+") as outfile: 
    outfile.write('lon lat Ve Vn Se Sn Cen Site Ref\n') 
    for line in infile.readlines(): 
     line = line.strip() 
     new_line=line + " "+percentage+" "+percentage+" "+'0.05 stat(0,0) test1'+'\n' 
     outfile.write(new_line) 
file1=raw_input()                
file2=raw_input()                
percentage=raw_input()               
convertfile(file1,file2,percentage) 

#!/bin/bash 
infile1=ux_source_$j.txt              
outfile1=ux_$j.txt                
percentage1=`sort biggest_z_amp | tail -1 | awk '{print $1*2e4}'`    
../convertfile.py<<!               
$infile1                  
$outfile1                  
$percentage1                 
!                    
infile2=uy_source_$j.txt              
outfile2=uy_$j.txt                
../convertfile.py<<!               
$infile2                  
$outfile2                  
$percentage1                 
!    
+1

“但我敢肯定,这个文件是在这个目录中”你真的确定吗?我认为这可能会失败的唯一原因是如果该文件真的不存在。 – CDspace

回答

1

这可能是你的问题在shell脚本中,而不是Python脚本。确保在给Python脚本输入的行中没有尾随空格。

更妙的是,使用方法:

file1 = raw_input().strip() 
file2 = raw_input().strip() 
+0

非常感谢。有用。请问在这里做什么“脱衣舞”?我在我的shell脚本中找不到可能导致此问题的任何错误。 – user2197705

+0

在你的shell脚本中,以'$ infile'开头的行有尾随空格。当Python脚本读取该行时,它认为输入文件的名称是“ux_source_2850.txt”(末尾有许多空格),而不仅仅是“ux_source_2850.txt”。 'strip'方法从字符串的开头和结尾删除白色字符。 – nickie

相关问题