2017-04-19 9 views
0

を読んで、次に書き:Pythonの - 私は、次のPythonコードを書いたファイル

# code that reads the file line by line 
def read_the_file(file_to_read): 
    f = open('test.nml','r') 
    line = f.readline() 
    print("1. Line is : ", line) 
    if '<?xml version="1.0"' in line: 
     next_line = f.readline() 
     print("2. Next line is : ", next_line) 
     write_f = open('myfile', 'w') 
     while '</doc>' not in next_line: 
      write_f.write(next_line) 
      next_line = f.readline() 
      print("3. Next line is : ", next_line) 
     write_f.close() 
    return write_f 

# code that processes the xml file 
def process_the_xml_file(file_to_process): 
    print("5. File to process is : ", file_to_process) 
    file = open(file_to_process, 'r') 
    lines=file.readlines() 
    print(lines) 
    file.close() 


# calling the code to read the file and process the xml 
path_to_file='test.nml' 
write_f=read_the_file(path_to_file) 
print("4. Write f is : ", write_f) 
process_the_xml_file(write_f) 

基本的には第1の書き込み、ファイルを読み取ろうとします。コードには次のエラーがあります。

TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper 

私が間違ってやっていることを修正する方法はありますか?ありがとう。

+1

完全なエラーメッセージを表示できますか? – shiva

+1

このエラーは、発生した行番号も示します。 –

+0

同じ問題を抱える最小限のコードを作成してください。 [mcve]の作成方法を参照してください。 –

答えて

0

read_the_fileのreturn write_freturn write_f.nameに置き換えます。

write_fがファイルハンドラオブジェクトであるため、ファイルハンドラオブジェクトではなく、ファイルの名前をprocess_the_xml_fileに渡す必要があります。

+1

これは仕事に感謝してくれたと思います。それ以上のクエリを返します。 – adrCoder

1

ここでの問題は、process_the_xml_fileメソッドで文字列ではなく、閉じたファイルハンドルを使用していることです。

read_the_fileは、ファイルハンドルではなくファイルハンドルを返します。

+1

'read_the_file'関数で' write_f.name'を返すことができます。 – poke

関連する問題