2013-08-28 98 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

相關問題