您好,我试图在Mac OS中使用Python运行多个OS命令
我想通过在终端中使用convert命令将图像转换为tiff文件。但是我有100张要转换的图像,因此我使Python程序变得更容易。
import os
import subprocess
files = os.listdir("/Users/woonie/Downloads/test")
i=1
for file in files:
args=['convert',file,'-resize','100%','-type',"Grayscale","/Users/woonie/Downloads/test/kor.",i,"test.exp0.tif"]
subprocess.Popen(args)
i = i+1
convert filename -...- output_filename.exp0.tif是convert命令的形式,因此我每次都需要更改文件名和output_filename。我在文件中有文件名列表。我想在“测试”之后放置图像数量,使其成为kor.test1.exp0.tif,kor.test2.exp0.tif等。
Traceback (most recent call last):
File "/Users/woonie/PycharmProjects/image_chage/change.py", line 9, in <module>
subprocess.Popen(args)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/subprocess.py", line 1637, in _execute_child
self.pid = _posixsubprocess.fork_exec(
TypeError: expected str, bytes or os.PathLike object, not int
但是这个错误出来了。
所以我将代码更改为
args=['convert'+str(file)+'-resize','100%','-type',"Grayscale","/Users/woonie/Downloads/test/kor."+str(i)+"test.exp0.tif"]
但是出现了同样的错误... 当我将该命令直接放入Terminal时,它已经开始工作。
我使用子流程有误吗?否则我无法按照自己的意愿制作程序
用
代替
That being said, your
args
array should probably look like因为文件名和调整大小选项可能是单独的参数。