最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python中os.stat报错:TypeError not all arguments converted during string formatting

Python crifan 3509浏览 0评论
折腾:
【未解决】Python更新文件最后修改时间
期间,报错:
发生异常: TypeError
not all arguments converted during string formatting
File "/Users/xxx/dev/crifan/gitbook/gitbook_template/common/tools/generate_md_from_summary.py", line 52, in updateFileTime
 statInfo = os.stat(filePath) File "/Users/xxx/dev/crifan/gitbook/gitbook_template/common/tools/generate_md_from_summary.py", line 96, in <module>
 curTimestamp = curDateTime.timestamp() # 1620549669.399425
python os.stat not all arguments converted during string formatting
Not all arguments converted during string formatting | Career Karma
TypeError: not all arguments converted during string formatting python – Stack Overflow
python os.stat not all arguments
Python os.stat not expanding wildcard in filename – Stack Overflow
Python parameter using arguments – Stack Overflow
os — Miscellaneous operating system interfaces — Python 3.9.5 documentation
此处就是普通的 str的文件路径,没有需要格式化还缺少参数的
python os.stat TypeError
image – Python TypeError: stat: path should be string, bytes, os.PathLike or integer, not list – Stack Overflow
python – TypeError: stat: path should be string, bytes, os.PathLike or integer, not _io.TextIOWrapper – Stack Overflow
python – stat: path should be string, bytes, os.PathLike or integer, not NoneType – refextract – Stack Overflow
python – PubNub: TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType – Stack Overflow
到最后,结果发现:
是自己搞错了,报错代码是:
print("statInfo=%s" % statInfo)
而不是:
statInfo = os.stat(filePath)
错误原因是:
此处statInfo是个特殊的对象,不能用%s去格式化
https://docs.python.org/3/library/os.html#os.stat_result
所以,算了,不print了:
# print("statInfo=%s" % statInfo)
后续用:
statInfo.st_atime
statInfo.st_mtime
即可。
【总结】
此处:
    statInfo = os.stat(filePath)
    print("statInfo=%s" % statInfo)
中报错:
发生异常: TypeError
not all arguments converted during string formatting
以为是:os.stat(filePath) 的错误。
实际上是:
print("statInfo=%s" % statInfo)
报错的。
原因:statInfo是一个特殊的Object对象
class os.stat_result
此处不能直接print,所以会报错。
解决办法:不要print 或者 打印对应子字段
print("statInfo.st_atime=%s" % statInfo.st_atime)
print("statInfo.st_mtime=%s" % statInfo.st_mtime)
即可。
相关文档:
https://docs.python.org/3/library/os.html#os.stat
https://docs.python.org/3/library/os.html#os.stat_result

转载请注明:在路上 » 【已解决】Python中os.stat报错:TypeError not all arguments converted during string formatting

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
92 queries in 0.181 seconds, using 23.36MB memory