【问题标题】:Python TypeError: coercing to Unicode: need string or buffer, tuple foundPython TypeError:强制转换为 Unicode:需要字符串或缓冲区,找到元组
【发布时间】:2015-05-03 18:37:17
【问题描述】:
#!/usr/bin/env python
import sys
import os

print "Scan a file for ""ErrorScatter"" payload"
print "Drag the suspicious file here then press enter."
filepath = raw_input("File Location: ")
fixpath = filepath , "/Contents/MacOS/ErrorScatter"
scan = os.path.exists(fixpath)

所以我正在制作一个程序来检查文件是否具有“ErrorScatter”有效负载,但在测试我的创作时我不断遇到错误。由于我是新手,我不知道如何解决这个问题。

这是我得到的错误:

TypeError: coercing to Unicode: need string or buffer, tuple found

有人知道如何解决这个问题吗?

【问题讨论】:

  • fixpath = filepath + "/Contents/MacOS/ErrorScatter"
  • 谢谢伙计。有效。 :D :D

标签: python operating-system tuples typeerror


【解决方案1】:

,Python中的运算符用于创建元组,例如

1, 2, 3

制作三元素元组

(1, 2, 3)

"blah", "bleh"

表示二元元组

("blah", "bleh")

要连接字符串,您可以将+ 用作Gaurav already suggested

fixpath = filepath + "/Contents/MacOS/ErrorScatter"

但实际上更好的方法是

import os

fixpath = os.path.join(filepath, "Contents/MacOS/ErrorScatter")

甚至

fixpath = os.path.join(filepath, "Contents", "MacOS", "ErrorScatter")

(使用os.path.join 是一种习惯,一旦您碰巧在 Windows 上运行了一些脚本,您就会喜欢这种习惯,这种可能性不大,但习惯会随着重复而养成……)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 2015-02-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多