尝试打开文件时出现TypeError

时间:2017-04-19 08:57:48

标签: python typeerror

我编写了以下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)

基本上尝试先写入然后再读取文件。该代码给出以下错误:

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

任何想法我做错了什么以及如何解决它?感谢。

2 个答案:

答案 0 :(得分:1)

这里的问题是你在process_the_xml_file方法中使用的是封闭文件句柄而不是字符串。

read_the_file返回文件句柄而不是文件名。

答案 1 :(得分:0)

将read_the_file中的<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional //EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="x-apple-disable-message-reformatting"> <meta http-equiv="Content-Type" content="text/html charset=UTF-8"> <link href="https://fonts.googleapis.com/css?family=Montserrat|Open+Sans:300" rel="stylesheet"> <title>New Event Template Test</title> </head> <body bgcolor="#f8f8f8" style="background:#f8f8f8;margin:0;padding:0;"> <a name="top" data-hs-link-id="0" target="_blank"></a> <!-- framing wrapper --> <table background="#f8f8f8" style="border-collapse:separate; padding-top:38px" width="100%" align="center" border="0" cellpadding="0" cellspacing="0"> <tbody> <tr> <td width="100%" align="center" class="body__table" style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif; border:0"> <!--[if mso]> <table width="600" align="center" cellpadding="0" cellspacing="0" border="0" class="wrapper__table" background="#f8f8f8"> <![endif]--> <!--[if !mso]><!-- --> <table width="100%" align="center" cellpadding="0" cellspacing="0" border="0" style="border-collapse:separate; background:#f8f8f8; max-width:600px" class="wrapper__table" background="#f8f8f8"> <!--<![endif]--> <tbody> <tr> <td style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif; border:10px solid #ffffff;"> <!-- hero --> <table width="100%" align="left" cellpadding="0" cellspacing="0" border="0" bordercolor="#ffffff" bordercolorlight="#ffffff" bordercolordark="#ffffff" class="hero__table" style="border-collapse:separate; background:#f5f7f6; border-color:#ffffff"> <tbody> <tr> <td class="hero__content" background="" style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif;border:30px solid #f5f7f6;"> <table class="hero__content--table" width="100%" align="left" cellpadding="0" cellspacing="0" border="0" bordercolor="#F5F7F6" bordercolorlight="#F5F7F6" bordercolordark="#F5F7F6" background="#F5F7F6" style="border-collapse:separate; background:#f5f7f6;"> <tbody> <tr> <td class="hero--subhead__column" style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif; border:0; padding-top:17px"> <div class="hero--subhead" style="font-weight:bold; font-size:14px; line-height:21px; text-transform:uppercase; letter-spacing:1.5px"><span class="block-mobile">Webinar: </span>03/08 at 10:00AM - 12:00PM PST</div> </td> </tr> <tr> <td class="heroHeaderColumn" style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif; border:0; padding-top:15px"> <h1 class="hero--header" style="font-size:36px; line-height:45px; margin:0"><div id="hs_cos_wrapper_hero_header" class="hs_cos_wrapper hs_cos_wrapper_widget hs_cos_wrapper_type_text" style="color: inherit; font-size: inherit; line-height: inherit;" data-hs-cos-general-type="widget" data-hs-cos-type="text">Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div></h1> </td> </tr> <tr> <td class="hero--button__column" style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif; border:0; padding-top:47px; padding-bottom:37px"> <a class="email-button" style="text-transform:uppercase; letter-spacing:1.8px; font-size:14px; font-weight:bold; text-decoration:none; background-color:#252525; border-left:38px solid #252525; border-right:38px solid #252525; border-top:12px solid #252525; border-bottom:12px solid #252525; background:#252525; color:#FFFFFF" href="http://google.com" bgcolor="#252525" data-hs-link-id="0" target="_blank">This is a button</a> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> <table width="100%" border="0" cellpadding="0" cellspacing="0"> <tr> <td style="font-family:'Montserrat', 'Lucida Sans', 'Lucida Sans Unicode', sans-serif; border:0"> <img class="hero__image" src="https://dummyimage.com/1160x676/cccbcb/252525.jpg&text=image" width="580" style="vertical-align:text-top; max-width:580px"> </td> </tr> </table> <!-- /framing wrapper --> </td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </body> </html> 替换为return write_f

write_f是文件处理程序对象,需要将文件名传递给process_the_xml_file,而不是文件处理程序对象。