【发布时间】:2021-09-15 12:19:04
【问题描述】:
你好,所以我正在使用 python 并想向文本文件写入一些内容,但它不起作用...... 我的代码的错误部分:
working_directory = Path(__file__).absolute().parent
profilename1 = Banana
getProductFile = (working_directory + profilename1+".txt")
with open(getProductFile, 'a') as f:
print('\n'.join(list), file=f)
print('\n'.join(list1), file=f)
当我运行它时,我得到了这个错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.2800.0_x64__qbz5n2kfra8p0\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\yvesb\OneDrive\Desktop\Script\SATURN.py", line 177, in getLogin
linkopen()
File "C:\Users\yvesb\OneDrive\Desktop\Script\SATURN.py", line 204, in linkopen
getProduct()
File "C:\Users\yvesb\OneDrive\Desktop\Script\SATURN.py", line 265, in getProduct
getProductFile = (working_directory + profilename1+".txt")
TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'
我该如何解决?
【问题讨论】: