python - 使用不同的变量运行 python 命令?
问题描述
我想用不同的变量执行 python 脚本。我必须从文件夹中批处理图像文件,但我只想从具有不同变量的脚本中执行。喜欢
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18. 19, 20]
!python autozoom.py --in ./images/1.jpg --out ./videos/1.mp4
!python autozoom.py --in ./images/2.jpg --out ./videos/2.mp4
!python autozoom.py --in ./images/3.jpg --out ./videos/3.mp4
!python autozoom.py --in ./images/4.jpg --out ./videos/4.mp4
等到20
对不起,我是python语言的绝对初学者。任何答案或建议我都会非常感激!:) 谢谢
解决方案
让我们谈谈如何autozoom.py
最好地编写来支持这种用例,而不需要在 20 次不同的时间启动新的 Python 解释器。在理想的世界中,它可能如下所示:
import argparse
def convert(infile, outfile):
pass # do the magic here
def main():
ap = argparse.ArgumentParser
ap.add_argument('--in', dest='infile', type=argparse.FileType('rb'))
ap.add_argument('--out', dest='outfile', type=argparse.FileType('wb'))
args = ap.parse_args()
convert(args.infile, args.outfile)
if __name__ == '__main__':
main()
...命令行解析代码调用执行实际工作的函数,但也可以直接调用该函数。
如果是这种情况,您使用该convert
函数的代码可能如下所示:
import autozoom
for i in range(1, 21):
with open(f'images/{i}.jpg', 'rb') as input_file, \
open(f'videos/{i}.mp4', 'wb') as output_file:
autozoom.convert(input_file, output_file)
推荐阅读
- mongodb - Mongoose - 通过引用字段 _id 查找
- python - 使用python读取excel文件时copy()函数出错
- javascript - 在本地主机服务器上运行 Ajax .POST 并且总是失败?
- sql-server - 这是从 SQL Server 中包含 10M 行的表中的日期时间字段获取 YearMonth 的最快方法
- java - Statistical Firebase crashlytics 让我感到困惑
- react-native - React - 导航到同一个组件的奇怪行为
- hybris - Hybris Backoffice 没有反映真实的数据库值
- java - 如何将txt文档中的字符串转换为代码?
- typescript - 结合映射类型区分已知值的联合
- javascript - 如何使用 select2 显示选定的国家/地区?