![](/img/trans.png)
[英]Which python script is executed if directory path is passed as command line argument to python?
[英]Python : want to use the command line argument passed in python, as an argument to another command that will be executed within the python script
我正在嘗試編寫一個 python 腳本,其中在傳遞給此腳本的參數中,需要用作將在此腳本中執行的命令的參數。 在 cygwin 上執行這個腳本
例如:- x.py file1
在 x.py 腳本中:
file = sys.argv[1]
data = os.popen('ar t file').read()
當這個命令被執行時,文件的值沒有被采用(即文件1),而是'file'變量被認為是一個文件,因此拋出一個錯誤:找不到文件名'文件'。是否可以通過文件的值作為此命令的參數。
謝謝,阿基萊什
這與命令參數或傳遞它們無關,而是對字符串的基本誤解。 您有一個包含“file”一詞的硬編碼字符串,您實際上並未在任何地方傳遞file
變量的值。 您可以使用字符串替換:
data = os.popen('ar t {}'.format(file)).read()
或者
data = os.popen('ar t %s' % file).read()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.