【问题标题】:Can't print french accents with python and subprocess [duplicate]无法使用python和子进程打印法语口音[重复]
【发布时间】:2020-06-06 03:23:14
【问题描述】:

我正在尝试使用

获取当前工作目录的文件和目录
info = subprocess.run("dir", shell=True, capture_output=True)
print(info.stdout)

但我有带有é 的文件,它们被打印为,...

我也有空闲字节的问题,我明白了:

49ÿ372ÿ921ÿ856 octets libres

我尝试对其进行解码,但收到错误消息AttributeError: 'std_output' object has no attribute 'decode'

显然它已经被解码了。

【问题讨论】:

  • 这是什么版本的 Python?想必您在这里使用的是 Windows?
  • 在带有 python 3.8.3 的 Windows 上是的

标签: python python-3.x subprocess


【解决方案1】:

重音 aigu "é" 在 UTF 字符串中实际上是两个字符。它是“´ - e”,所以这预期的行为。

此外,您不需要subprocess 来列出目录。你可以这样做:

import os
os.listdir(".")

【讨论】:

  • 谢谢,但我需要子进程来处理该程序中的其他内容
  • 所以我想解决子进程库的问题,我该怎么做?
  • 并且使用该命令我没有得到与dir 相同的输出,我没有文件大小等
  • 对于你的最后一个问题,这里是如何获得大小:[(f, os.path.getsize(f)) for f in os.listdir(".")] 运行一个 shell 子进程来处理你可以在原生 python 中轻松完成的事情通常被认为是一种不好的做法。
  • 如果你只需要运行一个shell命令,我猜subprocess.check_output就是你要找的。​​span>
猜你喜欢
  • 2022-11-21
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 2013-11-05
  • 2018-09-20
  • 2019-06-03
  • 1970-01-01
相关资源
最近更新 更多