AbuAlBaghadi
AbuAlBaghadi

Reputation: 31

TypeError - What does this error mean?

So, i've been writing this program that takes a HTMl file, replaces some text and puts the return back into a different file in a different directory.

This error happened.

    Traceback (most recent call last):
      File "/Users/Glenn/jack/HTML_Task/src/HTML Rewriter.py", line 19, in <module>
        with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):
              File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/posixpath.py", line 89, in join
        genericpath._check_arg_types('join', a, *p)
      File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/genericpath.py", line 143, in _check_arg_types
        (funcname, s.__class__.__name__)) from None
    TypeError: join() argument must be str or bytes, not 'TextIOWrapper'

Below is my code. Has anyone got any solutions I could implement, or should I kill it with fire.

    import re
    import os
    os.mkdir ("dest")

    file = open("2016-06-06_UK_BackToSchool.html").read()
    text_filtered = re.sub(r'http://', '/', file)
    print (text_filtered)

    with open ("2016-06-06_UK_BackToSchool.html", "wt") as out_file:
        print ("testtesttest")

    with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):
               out_file.write(text_filtered)

    os.rename("/Users/Glenn/jack/HTML_Task/src/2016-06-06_UK_BackToSchool.html", "/Users/Glenn/jack/HTML_Task/src/dest/2016-06-06_UK_BackToSchool.html")

Upvotes: 2

Views: 1530

Answers (2)

Vivek Sable
Vivek Sable

Reputation: 10213

with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):

Here out_file if TextIOWrapper, not string.

os.path.join takes string as arguments.


  1. Do not use keywords name as variable. file is keyword.
  2. Do not use space in between function call os.mkdir ("dest")

Upvotes: 2

Alessio
Alessio

Reputation: 153

try to change this:

with open ("2016-06-06_UK_BackToSchool.html", "wt") as out_file

on this:

with open ("2016-06-06_UK_BackToSchool.html", "w") as out_file:

or this:

with open ("2016-06-06_UK_BackToSchool.html", "wb") as out_file:

Upvotes: 1

Related Questions