使用Python代码关闭已启动的image/pdf文件

Ven*_*esh 2 python file python-2.7

我想在python中运行我的测试用例时启动并关闭image/pdf/word文件.我能够成功启动image/pdf/word文件.但是当我尝试关闭同一个文件时,我无法做到.python是否支持关闭已启动的文件?

我的要求是,我必须通过相机扫描计算机上的图像,当我的测试运行时,计算机屏幕上的计算机屏幕被固定.

我可以使用以下命令启动该文件:

import os
from os import path
os.system(path.join('C:\users\fakepath','image1.jpg'))
Run Code Online (Sandbox Code Playgroud)

如何关闭已启动的文件?

jh3*_*314 6

使用上下文是首选方式:

with open('filename','r') as f:
    # do stuff with file


#here, f will automatically be closed.
Run Code Online (Sandbox Code Playgroud)