我想將這個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
!
「但我敢肯定,這個文件是在這個目錄中」你真的確定嗎?我認爲這可能會失敗的唯一原因是如果該文件真的不存在。 – CDspace