我正在尝试编写一个程序,将两个列表合并在一起,删除重复项,并将结果写入我的(工作目录(。但我得到以下错误:
File "/opt/subdomain-hunter/src/enumerate.py", line 59, in enumerate
with open(MASTERLIST, "w") as f:
TypeError: expected str, bytes or os.PathLike object, not list
这是我最后几行代码:
# Read both text files and join them together
if os.path.exists('list1.txt'):
with open('list1.txt') as f:
list1 = f.read().splitlines()
f.close()
if os.path.exists('list2.txt'):
with open('list2.txt') as f:
list2 = f.read().splitlines()
f.close()
# Combine these into a MASTERLIST
MASTERLIST = list1 + list2
# Remove duplicates
MASTERLIST = list(set(MASTERLIST))
print('nn[+] MASTERLIST created!n')
with open(MASTERLIST, "w") as f:
f.write('../conf/ignore.txt')
根据您的回溯,您实际上调用的是带有列表MASTERLIST
的open
,而不是发布的代码中显示的filename
。列表不是open
的有效参数。您应该使用文件名调用open
,然后将MASTERLIST
的内容写入文件对象:
with open(filename, "w") as f:
f.write('n'.join(MASTERLIST))