How can i fix TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'
How can i fix TypeError: unsupported operand type(s) for +: 'WindowsPath' and 'str'
你好,我正在使用 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'
我该如何解决?
您必须将路径与 /
而非 +
:
组合
working_directory = Path(__file__).absolute().parent
profilename1 = Banana
product_file = working_directory / (profilename1 + ".txt")
with product_file.open('a') as output:
print('\n'.join(list), file=output)
print('\n'.join(list1), file=output)
使用Path.joinpath
或os.path.join
代替字符串连接
getProductFile = Path.joinpath(working_directory, profilename1 + ".txt")
getProductFile = os.path.join(working_directory, profilename1 + ".txt")
有两种方法可以将新路径部分连接到 pathlib.Path
、方法或重写的 /
getProductFile = working_directory.joinpath(profilename1 + ".txt")
getProductFile = working_directory / (profilename1 + ".txt")
然后你在with
块中重新定义f
,它应该更像
with open(getProductFile, 'a') as f:
print('\n'.join(list), file=f)
print('\n'.join(list1), file=f)
你好,我正在使用 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'
我该如何解决?
您必须将路径与 /
而非 +
:
working_directory = Path(__file__).absolute().parent
profilename1 = Banana
product_file = working_directory / (profilename1 + ".txt")
with product_file.open('a') as output:
print('\n'.join(list), file=output)
print('\n'.join(list1), file=output)
使用Path.joinpath
或os.path.join
代替字符串连接
getProductFile = Path.joinpath(working_directory, profilename1 + ".txt")
getProductFile = os.path.join(working_directory, profilename1 + ".txt")
有两种方法可以将新路径部分连接到 pathlib.Path
、方法或重写的 /
getProductFile = working_directory.joinpath(profilename1 + ".txt")
getProductFile = working_directory / (profilename1 + ".txt")
然后你在with
块中重新定义f
,它应该更像
with open(getProductFile, 'a') as f:
print('\n'.join(list), file=f)
print('\n'.join(list1), file=f)