简体   繁体   English

使用os.system将参数传递给python脚本时执行错误。 该脚本采用sys.argv参数

[英]Execution error when Passing arguments to a python script using os.system. The script takes sys.argv arguments

I have tried to execute a simple python command from cmd like C:\\Users> stat.py < swagger.yaml > output.html , which executes stat.py by taking swagger.yaml as input argument and generates output.html file and it worked fine in cmd. 我试图从cmd执行一个简单的python命令,例如C:\\Users> stat.py < swagger.yaml > output.html ,该命令通过将swagger.yaml作为输入参数来执行stat.py并生成output.html文件,并且在cmd中工作正常。 But now i want to execute my stat.py file through another python file demo.py by passing the values swagger.yaml and output.html as sys.argv[0] and sys.argv[1] inside demo.py. 但是现在,我想通过另一个python文件demo.py通过将值swagger.yaml和output.html传递为demo.py内的sys.argv [0]和sys.argv [1]来执行stat.py文件。

my command from cmd C:\\Users> demo.py swagger.yaml output.html and my demo.py file is as follows.. 我从cmd C:\\Users> demo.py swagger.yaml output.html和我的demo.py文件如下。

 # my demo.py file ....

import os
import sys

os.system('stat.py < sys.argv[1] > sys.argv[2]')

error - the system can not find the file specified. 错误-系统找不到指定的文件。
Why i am getting this error and please any help to resolve it .. 为什么我收到此错误,请提供任何帮助以解决它..

Inside a normal string, no variable interpretation is applied. 在普通字符串中,不应用任何变量解释。 So you literally asked to read from a file named sys.argv[1] (possibly sys.argv1 if the file exists, thanks to shell globbing), and write to a file named sys.argv[2] . 因此,您确实要求从名为sys.argv[1]的文件中读取文件(如果存在该文件,则可能是sys.argv1 ,这要归功于shell锁定),然后写入名为sys.argv[2]的文件中。

If you want to use the values sys.argv in your script, you need to format them into the string, eg with f-strings (modern Python 3.6 or so only): 如果要在脚本中使用值sys.argv ,则需要将其格式化为字符串,例如使用f字符串(仅适用于现代Python 3.6左右):

os.system(f'stat.py < {sys.argv[1]} > {sys.argv[2]}')  # Note f at beginning of literal

or on older Python 2.7, with str.format : 或使用str.format旧版Python 2.7:

os.system('stat.py < {} > {}'.format(sys.argv[1], sys.argv[2]))

Note that however you slice it, this is dangerous; 请注意,无论切片多少,这都是很危险的。 os.system is launching this in a shell, and arguments that contain shell metacharacters will be interpreted as such. os.system在shell中启动它,包含shell元字符的参数将被这样解释。 It can't do anything the user didn't already have permission to do, but small mistakes by the user could dramatically change the behavior of the program. 它无法执行用户尚未获得许可的任何操作,但是用户的小错误可能会极大地改变程序的行为。 If you want to do this properly/safely, use subprocess , open the files yourself, and pass them in explicitly as stdin / stdout : 如果要正确/安全地执行此操作,请使用subprocess ,自己打开文件,然后以stdin / stdout显式传递它们:

with open(sys.argv[1], 'rb') as infile, open(sys.argv[2], 'wb') as outfile:
    subprocess.run(['stat.py'], stdin=infile, stdout=outfile)

This ensures the files can be opened in the first place before launching the process, doesn't allow the shell to interpret anything, and avoids the (minor) expense of launching a shell at all. 这样可以确保在启动该过程之前首先可以打开文件,不让Shell解释任何内容,并且完全避免了启动Shell的(次要)开销。 It's also going to give you more useful errors if opening the files fails. 如果打开文件失败,它将也给您更多有用的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM