所以,我一直在编写这个程序,它接受一个HTMl文件,替换一些文本并将返回放回另一个目录中的不同文件。
发生此错误。
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'
以下是我的代码。有没有人得到我可以实施的任何解决方案,或者我应该用火来杀死它。
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")
答案 0 :(得分:2)
with open (os.path.join("/Users/Glenn/jack/HTML_Task/src", out_file)):
out_file
如果TextIOWrapper
,则不是字符串。
os.path.join
将字符串作为参数。
file
是关键字。os.mkdir ("dest")
答案 1 :(得分:1)
尝试改变这个:
with open ("2016-06-06_UK_BackToSchool.html", "wt") as out_file
就此:
with open ("2016-06-06_UK_BackToSchool.html", "w") as out_file:
或者这个:
with open ("2016-06-06_UK_BackToSchool.html", "wb") as out_file: