【发布时间】:2018-09-10 08:14:33
【问题描述】:
我想遍历文件夹中的一些文件并将文件的路径发送到列表。然后我想将此列表传递给子进程以执行 bash 命令:
procfiles = []
os.chdir("/path/to/directory")
for root, dirs, files in os.walk('.'):
for file in files:
if '.mp3' in file:
filename = os.path.join(root, file)
print(filename)
procfiles.append(filename)
print(procfiles)
args = [command, with, arguments].extend(procfiles)
process = subprocess.Popen(args, shell=False)
output, error = process.communicate()
但是当文件包含德语变音字母时,我得到以下输出。例如:ä、ö 或 ü
./titleWith ä or ü - artist with ü.mp3 #print(filename)
['./titleWith \udcc3\udca4 or \udcc3\udcbc - artist with \udcc3\udcbc.mp3'] #print(procfiles)
这意味着在编码过程中出现了问题
procfiles.append(filename) 进程,对吧?
之后子进程失败并出现错误:
UnicodeEncodeError: 'utf-8' codec can't encode character '\udcc3' in 位置 43:不允许代理
信息:
- Python 3.5.3
- 操作系统:Debian Jessie
- 内核:4.9.58+
- 架构:armhf
更新:
我刚刚注意到,当我使用用户 root 或 www-data 手动执行它时,它可以工作,但是当我通过我的 custom php 执行它时脚本(它只是一个shell_exec('/usr/bin/python3 /path/to/script.py >> /path/to/log.log 2>&1'))它不起作用。
这不应该和我从用户 www-data 手动执行它时一样吗?还是在从 php 脚本执行 python 脚本时设置了其他一些环境变量?
【问题讨论】:
标签: python python-3.x subprocess