gpt4 book ai didi

python - TypeError - 这个错误是什么意思?

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:41 25 4
gpt4 key购买 nike

所以,我一直在编写这个程序,它接受一个 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")

最佳答案

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

这里 out_file 如果 TextIOWrapper,不是字符串。

os.path.join 将字符串作为参数。


  1. 不要使用关键字名称作为变量。 file 是关键字。
  2. 不要在函数调用之间使用空格 os.mkdir ("dest")

关于python - TypeError - 这个错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38148337/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com