这是在运行 Python 3.8.5 和 3.90rc1 的 Windows 10 Linux (wsl) 中。不使用 Windows 本机。
我正在浏览一个目录层次结构,该目录层次结构首先由专辑标题列出的许多录制的专辑组成,并且在该标题中有一个按曲目顺序排列的歌曲列表。我正在尝试使用歌曲的全名并将其输出到 Popen 进程,该进程打开一个外部程序 /usr/bin/flac -ts 扫描单个文件并报告(通常通过 stderr)如果它以任何方式有缺陷的。曲目都是 .FLAC 文件。我已经尝试了多种方法来使用此处和其他站点的示例技术来完成此操作。不管怎样,总会有一些问题,比如 flac 程序永远无法获取文件名,或者导致 Python 因(看似)一些奇怪的错误而失败。这是我当前版本的摘录:
for path, dirs, files in os.walk(StartScanDir):
i=0
runout=[ ]
# runout[i]=path
runout.append(path)
print (runout[i])
# print (path)
for f in files:
if not f.endswith('.flac'):
continue
i=i+1
# print(i)
runout.append(f)
print (runout[i]) # > FLAClistOut
# print ("f")
pipe=Popen("/usr/bin/flac","-ts", stdout=PIPE, stderr=PIPE)
text = pipe.communicate()[0]
# text = pipe.communicate(b"f")
print(text)
print (stdout)
print (stderr)
这从 /usr/lib/python3.9/subprocess.py 'bufsize must be an integer' 引发了异常。我有时会明白这一点,但在我的编码的其他变体中,我收到关于输入需要二进制并获得 str 的抱怨。这在任一版本的 Python 上都是相同的行为。
谁能发现我可能做错了什么和/或提出更好的方法?有一些行被注释掉,只是为了测试。
谢谢。
我已经更新并评论了代码,所以发生了什么更清楚。
os.chdir(StartScanDir) #start at the top of the directory hierarchy
# print(os.getcwd())
for path, dirs, files in os.walk(StartScanDir):
i=0
runout=[ ] #clear logging string array
runout.append(path) #add album title
print (runout[i])
for f in files:
if not f.endswith('.flac'):
continue
i=i+1
runout.append(f) #add individual song titles
cmd = ['/usr/bin/flac','-t','-s']
print (runout[i]) # > FLAClistOut
p=subprocess.Popen(cmd, stdout = subprocess.PIPE, stdin = subprocess.PIPE)
out,err=p.communicate('f' )
print (out)
print (err)
This produces the error:
Traceback (most recent call last):
File "/mnt/x/incoming/py-work/fixflac.py", line 35, in <module>
out,err=p.communicate('f' )
File "/usr/lib/python3.9/subprocess.py", line 1130, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.9/subprocess.py", line 1957, in _communicate
input_view = memoryview(self._input)
TypeError: memoryview: a bytes-like object is required, not 'str'